【发布时间】:2015-02-22 17:42:16
【问题描述】:
我有一个这样的链表:
1,jhon,19
2,萨拉,18
3,tom,20
4,杰克,22
我一直在尝试根据他们的 id 删除一个元素(女巫是第一个数字)。但为了做到这一点,我需要从任何位置删除这个元素。所以我想出了这段代码,我想知道它是否正确:
temp1=head;
if(head!=NULL && head->id==givenID) // if the element is in the first position
{
temp = head;
head = head->next;
free(temp);
}
else if(head!=NULL && head->id!=givenID){// search for the element in the middle
do{
temp2=head;
head = head->next;
}while(head->id !=givenID && head->next !=NULL);
if(head->next !=NULL && head->id==givenID){// if the element is in the middle
temp2->next=head->next;
free(head);
head=temp1;
}
else if(head->next ==NULL && head->id==givenID){// if the element is in the last position
temp->next=NULL;
free(head);
head=temp1;
}
}
谢谢
【问题讨论】:
-
我认为它适用于代码审查网站
标签: c list linked-list