C language realizes the node insertion of the bidirectional non-cyclic chain list (leading node end node)

Source: Internet
Author: User

For two-way linked lists, it is more convenient for individuals to recommend the way to handle the end points of the lead node. I in the "C language to achieve two-way non-circular chain list (not the lead node) of the insertion of the nodes" in detail to achieve in the case of no lead node insertion. This time we will be going to insert the element at any location using the end node of the head node. Code uploaded to Https://github.com/chenyufeng1991/InsertNodeDoubleLinkedList_HeadNode.

The core code is as follows:

Insert a node
//insertion position for 0,1,2,.....
int insertnodelist (Node *phead,node *ptail,int pos,int x) {

    int i = 0;
    Node *pmove;
    Node *pinsert;
    Pinsert = (node *) malloc (sizeof (node));
    memset (pinsert, 0, sizeof (Node));
    Pinsert->prior = NULL;
    Pinsert->next = NULL;
    pinsert->element = x;

    Pmove = Phead;
    while (Pmove!= ptail) {
        if (i = = pos) {
            //Note the link order here
            pmove->next->prior = Pinsert;
            Pinsert->next = pmove->next;
            Pmove->next = Pinsert;
            Pinsert->prior = Pmove;

            printf ("%s function executes, insert x=%d node in pos=%d position \ n", __function__,pos,x);
            return 1;
        }
        i++;
        Pmove = pmove->next;
    }

    printf ("%s function execution, insert element failed \ \ \ __function__");

    return 0;
}


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.