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

Source: Internet
Author: User

I was in a previous blog "C Language implementation of single-linked list (not the lead node) insertion of nodes" specifically, how to do in a non-lead node in a single linked list of the insertion of nodes. But in practical applications, the list of leading nodes is more frequently used. More convenient. Today we're going to use a single-linked list of lead nodes for node insertion. The demo sample code is uploaded to Https://github.com/chenyufeng1991/InsertList_HeadNode.

The core code is as follows:

Node *insertnode (node *pnode,int pos,int x) {    int i = 0;    Node *pmove;    Node *pinsert;    Pinsert = (node*) malloc (sizeof (Node));    memset (pinsert, 0, sizeof (Node));    Pinsert->next = NULL;    pinsert->element = x;    Pmove = Pnode;    There is no need to consider the first node separately while    (pmove! = NULL) {        if (i = = pos) {            Pinsert->next = pmove->next;            Pmove->next = Pinsert;            printf ("%s function runs, insert value=%d successfully in pos=%d location \ n", __function__,pos,x);            return pnode;        }        i++;        Pmove = pmove->next;    }    printf ("%s" function runs.) Failed to insert node \ n ", __function__);    return pnode;}


C language implementation of single-linked table node insertion (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.