我正在学习 C 中的链表,我的删除函数有问题,一直给我一个分段错误。我不知道代码有什么问题。
void delete(int d)
{
struct list * current1 = head;
struct list * current2;
if (len() == 0)
{ //prtError("empty");
exit(0);
}
if (head -> data == d)
{
head = head -> next;
}
//Check if last node contains element
while (current1->next->next != NULL)
current1 = current1->next;
if(current1->next->data == d)
current1->next == NULL;
current1 = head; //move current1 back to front */
while(current1 != NULL && (current1->next->data != d))
current1 = current1 -> next;
current2 = current1 -> next;
current1 -> next = current2 -> next;
}