C Implementation of the head interpolation and the tail interpolation method to build a two-way non-circular chain list (leading node end node) __ doubly linked list

Source: Internet
Author: User

If there is a head node and a tail node in a bidirectional linked list, it is very convenient for the head interpolation and the tail interpolation method. This inserts an element into the tail without having to traverse the list. It is recommended that you use this linked list to handle problems. Code uploaded to Https://github.com/chenyufeng1991/HeadInsertAndTailInsert_DoubleList_HeadList.

The core code is as follows:

Create a bidirectional non-cyclic chain table (header interpolation) void Headinsertcreatelist (node *phead,node *ptail) {node *pinsert for the lead node and tail node;
    Pinsert = (node *) malloc (sizeof (node));
    memset (pinsert, 0, sizeof (Node));
    Pinsert->prior = NULL;

    Pinsert->next = NULL;
    scanf ("%d",& (pinsert->element));
        while (Pinsert->element > 0) {phead->next->prior = Pinsert;
        Pinsert->next = phead->next;
        Pinsert->prior = Phead;

        Phead->next = Pinsert;
        Pinsert = (node *) malloc (sizeof (node));
        memset (pinsert, 0, sizeof (Node));
        Pinsert->prior = NULL;

        Pinsert->next = NULL;
    scanf ("%d",& (pinsert->element));
printf ("%s function completed, header interpolation to establish the leading node and the trailing nodes of the bidirectional non circular chain table creation succeeded \ n", __function__);
    ///Create the bidirectional non-cyclic link table (tailinsertcreatelist) Void (node *phead,node *ptail) {node *pinsert) of the lead node and the tail node;
    Pinsert = (node *) malloc (sizeof (node));
    memset (pinsert, 0, sizeof (Node));
    Pinsert->prior = NULL; Pinsert->next = NULL
    scanf ("%d",& (pinsert->element));
        while (Pinsert->element > 0) {ptail->prior->next = Pinsert;
        Pinsert->prior = ptail->prior;
        Pinsert->next = Ptail;

        Ptail->prior = Pinsert;
        Pinsert = (node *) malloc (sizeof (node));
        memset (pinsert, 0, sizeof (Node));
        Pinsert->prior = NULL;

        Pinsert->next = NULL;
    scanf ("%d",& (pinsert->element));
printf ("%s function completed, trailing interpolation to establish the leading node and the end point of the bidirectional Non circular chain table creation succeeded \ n", __function__);
 }

The test code is as follows:

int main (int argc, const char * argv[]) {

    node *phead;//header node
    *ptail;//tail node

    initiallist (&phead, & Ptail);

    Headinsertcreatelist (Phead, ptail);
    Printlist (Phead, ptail);
    Tailinsertcreatelist (Phead, ptail);
    Printlist (Phead, ptail);


    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.