嘿我创建了一个将输入值插入到排序链表中的函数,现在我正在尝试创建另一个函数来删除链表中输入的值。目前我正在获得rick james的无限循环,令人难以置信的令人沮丧。
typedef int ListItemType; // global value in my header file
ListItemType item; // assigned in head file under ListNode struct
bool List::remove(const ListItemType& removedItem) {
ListNode *curr = head;
ListNode *prev = NULL;
//empty list
if(head == NULL){
cout<< "No items in the list";
}else{
//traverse the list
while(curr != NULL){
if(curr->item == removedItem){
break; //data has been found break loop
}else{
//increment loop
prev = curr;
curr = curr->next;
}
}//end while
//data has not been found
if(curr == NULL){
cout << "RICK JAMES";
}else{
//data has been found delete data
// case 1: delete at head node
if(head == curr){
head = head->next;
}else{
// case 2: delete after head
prev->next = curr->next;
}
delete curr;
size--;
return true;
}
}
return false;
}
答案 0 :(得分:0)
功能逻辑对我来说没问题......
如果你一直得到“RICK JAMES”,这意味着它找不到要删除的节点 - 将节点的值与函数参数进行比较的方式可能有问题......
函数接受
ListItemType&安培; removedItem
对于int数据类型似乎有点奇怪,但我不确定ListNode中定义的“item”字段是什么...我建议仔细检查一下。祝你好运!