Cho em hỏi tại sao hàm addAfter có thể in ra được Can’t find the value còn hàm addBefore thì chương trình dừng luôn chứ không in ra gì ạ, em xin cảm ơn.
hàm addAfter dùng để thêm giá trị y vào sau giá trị x, còn hàm addBefore là dùng để thêm giá trị y vào trước giá trị x ạ.
#include <iostream>
using namespace std;
struct DNode
{
int info;
DNode *pNext, *pPrev;
};
struct DList
{
DNode *pHead, *pTail;
};
typedef DNode Node;
typedef DList List;
Node *GetNode(int x)
{
Node *p = new Node;
if (p != NULL)
{
p->info = x;
p->pNext = p->pPrev = NULL;
}
return p;
}
void init(List &L)
{
L.pHead = L.pTail = NULL;
}
void addHead(List &L, int x)
{
Node *newele = GetNode(x);
if (L.pHead == NULL)
{
L.pHead = L.pTail = newele;
}
else
{
newele->pNext = L.pHead;
L.pHead->pPrev = newele;
L.pHead = newele;
}
}
void addTail(List &L, int x)
{
Node *newele = GetNode(x);
if (L.pHead == NULL)
{
L.pHead = L.pTail = newele;
}
else
{
L.pTail->pNext = newele;
newele->pPrev = L.pTail;
L.pTail = newele;
}
}
void addAfter(List &L, int x, int y)
{
int flag = 0;
if (L.pHead == NULL)
return;
Node *p = L.pHead;
Node *newele = GetNode(y);
while (p != NULL)
{
if (p->info == x)
{
flag = 1;
newele->pNext = p->pNext;
newele->pPrev = p;
if (p == L.pTail)
{
L.pTail = newele;
}
else
{
p->pNext->pPrev = newele;
p->pNext = newele;
}
break;
}
p = p->pNext;
}
if (flag == 0)
{
cout << "\nCan't find the value " << x;
return;
}
}
void addBefore(List &L, int x, int y)
{
int flag = 0;
if (L.pHead == NULL)
{
return;
}
Node *p = L.pHead;
Node *newele = GetNode(y);
while (p != NULL)
{
if (p->pNext->info == x)
{
flag = 1;
newele->pNext = p->pNext;
newele->pPrev = p;
if (p == L.pTail)
{
L.pTail = newele;
}
else
{
p->pNext->pPrev = newele;
p->pNext = newele;
}
return;
}
p = p->pNext;
}
if (flag == 0)
{
cout << "\nCan't find the value " << x;
return;
}
}
void createList(List &L)
{
int a, n;
while (1)
{
cin >> a;
if (a == -1)
break;
else
addTail(L, a);
}
}
void printList(List L)
{
if (L.pHead == NULL)
{
cout << "List is empty";
return;
}
Node *p = L.pHead;
while (p != NULL)
{
cout << p->info << " ";
p = p->pNext;
}
}
int main()
{
DList L;
init(L);
int x, y, choice;
cout << "MENU:";
cout << "\n1. Create a DList";
cout << "\n2. Print the DList";
cout << "\n3. Insert a value at the front";
cout << "\n4. Insert a value at the end";
cout << "\n5. Insert a value after a given value (only for the first value found)";
cout << "\n6. Insert a value before a given value (only for the first value found)";
cout << "\n7. Insert a value after a given value (for all the same values)";
cout << "\n8. Insert a value before a given value (for all the same values)";
cout << "\n20. Exit" << endl;
while (true)
{
cout << "\n\t\tPLEASE SELECT YOUR CHOICE: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "\nEnter your positive integers until you enter -1 to finish: ";
createList(L);
break;
case 2:
cout << "\nYour current DList: ";
printList(L);
break;
case 3:
cout << "\nEnter a number: ";
cin >> x;
addHead(L, x);
break;
case 4:
cout << "\nEnter a number: ";
cin >> x;
addTail(L, x);
break;
case 5:
cout << "\nEnter two numbers: ";
cin >> x >> y;
addAfter(L, x, y);
break;
case 6:
cout << "\nEnter two numbers: ";
cin >> x >> y;
addBefore(L, x, y);
break;
case 20:
cout << "\nGOOD BYE";
return 0;
}
}
return 0;
}
mọi người có thể nhập 2 test này để kiểm tra ạ
1
1 2 3 4 5 -1
6
0 0
20
1
1 2 3 4 5 -1
5
0 0
20