C-language implementation of single-linked list (without the lead node) insertion of nodes

Source: Internet
Author: User

It is the most important operation to increase and revise the single-linked list. I was in the previous blog, "C Language Implementation linked list node deletion" to remove a single linked list of a node.

We're going to come here now. Insert a node in a location. The demo sample code is uploaded to Https://github.com/chenyufeng1991/InsertList.

The core code is as follows:

Node *inserttoposition (node *pnode,int pos,int x) {    if (pos < 0 | | pos > sizelist (pnode)) {        printf ("%s" function run, p os=%d illegal, inserting data failed \ n ", __function__,pos);        return pnode;    }    Node *pmove;    Node *pinsert;    Pinsert = (node *) malloc (sizeof (node));    memset (pinsert, 0, sizeof (Node));    Pinsert->next = NULL;    pinsert->element = x;    Pmove = Pnode;    int i = 1;    Here the case of Pos=0 is considered separately    if (pos = = 0) {        pinsert->next = Pnode;        Pnode = Pinsert;        printf ("%s" function, x=%d succeeded in pos=%d insert \ n ", __function__,pos,x);        return pnode;    }    while (pmove! = NULL) {        if (i = = pos) {            Pinsert->next = pmove->next;            Pmove->next = Pinsert;            printf ("%s" function runs.) In pos=%d insert x=%d success \ n ", __function__,pos,x);            break;        }        i++;        Pmove = pmove->next;    }    return pnode;}


C-language implementation of single-linked list (without the lead node) insertion of nodes

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.