Non-cyclic double linked lists without the leading nodes are troublesome when deleting nodes, because the prior and next two pointers are maintained. Both the first node and the last node should be considered separately, while the number of nodes should be considered 1. The deletion is divided into the following two categories:
(1) Delete the node of POS position;
(2) Determine if x is in the linked list, if it exists then delete;
Code uploaded to Https://github.com/chenyufeng1991/DeleteNodeDoubleList.
The core code is as follows:
Delete node nodes *deleteposlist of POS Locations {//Note that the first node and the last node int i = 1 need to be considered separately;
int size = Sizelist (Pnode);
Node *pmove;
Pmove = Pnode;
List is null if (Pnode = null) {printf ("%s function execution, list NULL, delete node failed \ \", __function__);
return pnode;
//List only one node, delete the first node if (pos = = 1 && size = = 1) {free (pnode);
Pnode = NULL;
printf ("%s function execution, delete pos=%d location node succeeded \ \", __function__,pos);
return NULL;
}//Linked list node is greater than 1, delete the first node if (pos = = 1) {Pnode = pnode->next;
Pnode->prior = NULL;
Free (pmove);
Pmove = NULL;
printf ("%s function execution, delete pos=%d location node succeeded \ \", __function__,pos);
return pnode; while (Pmove!= NULL) {if (i = = pos && pos!= size) {Pmove->prior->next = Pmov
e->next;
Pmove->next->prior = pmove->prior;
Free (pmove);
Pmove->next = NULL; printf ("%s function execution, deleting P"os=%d location node succeeded \ n ", __function__,pos);
return pnode;
} if (i = = pos && pos = size) {Pmove->prior->next = NULL;
Free (pmove);
Pmove = NULL;
printf ("%s function execution, delete pos=%d location node succeeded \ \", __function__,pos);
return pnode;
} i++;
Pmove = pmove->next;
printf ("%s function execution, deletion of node in pos=%d location failed \ \", __function__,pos);
return pnode;
}//To determine if X is in the linked list, delete the node *deletevaluelist (node *pnode,int x) {node *pmove;
Pmove = Pnode;
int size = Sizelist (Pnode);
The original list is null if (Pnode = null) {printf ("%s function execution, original linked list is empty, delete node failed \ \", __function__);
return NULL;
//delete is the first node, and the linked list length is 1 if (pnode->element = x && size = = 1) {free (pnode);
Pnode = NULL;
printf ("%s function execution, deletion of%d node succeeded \ \", __function__,x);
return pnode;
//Consider deleting the first node separately, and the list length is greater than 1 if (pnode->element = x) {Pnode = pnode->next; Free (pnode->prior);
Pnode->prior = NULL;
printf ("%s function execution, deletion of%d node succeeded \ \", __function__,x);
return pnode; } while (Pmove!= null) {//the node to be deleted is not the last if (pmove->element = = x && pmove->next!= null
) {Pmove->prior->next = pmove->next;
Pmove->next->prior = pmove->prior;
Free (pmove);
Pmove = NULL;
printf ("%s function execution, deletion of%d node succeeded \ \", __function__,x);
return pnode; //To be deleted is the last node if (pmove->element = = x && pmove->next = NULL) {PMOVE->PR
Ior->next = NULL;
Free (pmove);
Pmove = NULL;
printf ("%s function execution, deletion of%d node succeeded \ \", __function__,x);
return pnode;
} Pmove = pmove->next;
printf ("%s function execution, deletion value%d node failed \ \ __function__,x");
return pnode;
}