C language implementation of single-linked table node deletion (lead node)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.