我有一个这样的链表:
1,JHON,19
2,SARA,18
3,公鸡,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;
}
}
谢谢
答案 0 :(得分:3)
这段代码太复杂了,因为它有不必要的分支。您可以使用指针指针来统一代码。
想法是将指针指向指向列表的head
的指针,然后指向列表的初始元素的next
指针,然后指向next
的指针。列表的第二个元素,依此类推。这种方法的优点在于,无论您在列表中的哪个位置,指针指针的操作都保持不变!
以下是代码中的外观:
// Point your pointer to pointer to the head of the list
struct node **pptr = &head;
while (*pptr != NULL) {
// Dereference pptr to get the pointer to current node
node *current = *pptr;
// Check if the id of this node matches what we're looking for
if (current->id == givenID) {
// Here is the "magic": assign the next pointer of the current node
// to whatever is pointed to by pptr.
// It could be a head, or a next of some node.
*pptr = current->next;
free(current);
break;
}
pptr = &(current->next);
}
就是这样!由于指向指针的指针不区分头部和其他节点,因此不会进行额外的检查。
答案 1 :(得分:0)
考虑使用哨兵节点。当你这样做时,所有特殊情况都会消失 这就是节点擦除在带有哨兵的链表中的样子:
Iterator Erase( List* lst, Iterator here )
{
Iterator nxt = here->next;
Link( here->prev, here->next );
free( here );
lst->size -= 1;
return nxt;
}
Link并不比
复杂void Link( Iterator n1, Iterator n2 )
{
n1->next = n2;
n2->prev = n1;
}
所有其他核心功能,如插入等,同样微不足道。