I was in a previous blog "C Language implementation of single-link node deletion (do not take the lead)" in the specific implementation of a single link in a non-lead node of the deletion of a nodes, in this blog I changed to the lead node of the single linked list. The Code demo sample is uploaded to Https://github.com/chenyufeng1991/DeleteLinkedList_HeadNode. There are two types of delete:
(1) Delete a location pos node;
(2) Infer whether the X value is in the linked list, or delete the node if it exists;
The core code is as follows:
Delete a location POS node *deleteposnode (node *pnode,int pos) {int i = 1; Node *pmove; Node *pmovepre; Pmovepre = Pnode; Pmove = pnode->next; while (pmove! = NULL) {if (i = = pos) {Pmovepre->next = pmove->next; Free (pmove); Pmove = NULL; printf ("%s" function runs.) Delete node at pos=%d location \ n ", __function__,pos); return pnode; } i++; Pmovepre = pmovepre->next; Pmove = pmove->next; } printf ("%s" function, failed to delete node at pos=%d location \ n ", __function__,pos); return pnode;} Infer if the X value is in the linked list, and if it exists, delete the node *deletevaluenode (node *pnode,int x) {node *pmovepre; Node *pmove; Pmovepre = Pnode; Pmove = pnode->next; while (pmove! = NULL) {if (pmove->element = = x) {Pmovepre->next = pmove->next; Free (pmove); Pmove = NULL; printf ("%s function run, delete value=%d node succeeded \ n", __function__,x); return pnode; } Pmovepre = pmovepre->next; Pmove = pmove->next; } printf ("%s function run, delete value=%d node failed \ n", __function__,x); return pnode;}
C language implementation of single-linked table node deletion (lead node)