Chào mọi người ạ, em đang học danh sách liên kết c++ ạ. Khi em tạo hàm xoá tất cả các phần tử giaTri trong DS thì bị sai, cụ thể là trường hợp nhập
7(pHead) - 7 - 4 - 5
và em muốn xoá hết 7 ra khỏi linked list
khi thực thi thì code chỉ xoá node pHead chứ không hết. Code của em dưới đây:
void RemoveAll (T giaTri) { // Xoá hết giaTri khỏi danh sách (Nếu có)
Node *pNodeTruocDo = NULL;
for (Node *t = pHead; t != NULL; t = t->pNext) {
if (t->data == giaTri) {
if (pNodeTruocDo == NULL) { // Trường hợp x là nút đầu
Node *temp = t;
pHead = pHead->pNext;
t = pHead;
delete temp;
}
else {
Node *temp = t;
pNodeTruocDo->pNext = t->pNext;
t = t->pNext;
delete temp;
}
size--;
}
if (pHead->data != t->data) {
pNodeTruocDo = t;
}
}
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?