C 双链表删除函数中的代码缩减
我的问题是:C 双链表删除函数中的代码缩减,c,doubly-linked-list,C,Doubly Linked List,我的问题是: 有没有办法减少双链表中的代码delete函数?这是我的实现,不是更短,但您可以检查差异。而且更干净。p为头部,g为尾部节点 void delete(struct node **top,int val) { struct node *cur=(*top); while(cur!=NULL&&(cur)->data!=val) { cur=cur->next;
有没有办法减少双链表中的代码
delete
函数?这是我的实现,不是更短,但您可以检查差异。而且更干净。p为头部,g为尾部节点
void delete(struct node **top,int val)
{
struct node *cur=(*top);
while(cur!=NULL&&(cur)->data!=val)
{
cur=cur->next;
if(cur==(*top))
{
if((*top)->next!=NULL)
{
(*top)=(*top)->next;
(*top)->prev=NULL;
}
else
(*top)=NULL;
}
else
{
cur->prev->next=cur->next;
if(cur->next!=NULL)
cur->next->prev=cur->prev;
}
free(cur);
printf("deleted %d \n",val);
}
}
void Container::Remove(int pr)
{
Node *pDel = p; Node *pNex = p->next;
while (pDel != NULL){
if (pDel->item.GetValue() > pr){
if (p == NULL || pDel == NULL) return;
if (p == pDel) p = pDel->next;
if (g == pDel) g = pDel->prev;
if (pDel->next != NULL)
pDel->next->prev = pDel->prev;
if (pDel->prev != NULL)
pDel->prev->next = pDel->next;
delete pDel;
}
pDel = pNex;
if(pDel != NULL) pNex = pDel->next;
}
}