Establishment and basic operation of doubly linked list

Source: Internet
Author: User

A doubly linked list has greater flexibility than a single-linked list, and most of its operations are the same as linear tables. The following summarizes the differences between the two-way linked list and the single-linked list and the problems I encountered in the implementation process.

1. Establishment of a doubly linked list
When initializing a doubly linked list, allocate memory space to the first two nodes. After a successful allocation, it is a critical step to point the first node's prior pointer and the next pointer of the tail node to NULL, because this is the condition that is then used to determine the empty table. At the same time, when the list is empty, the first node's next point is pointed to the tail node, and the prior of the tail node points to the first node.

2. Insert operation for doubly linked list
Because a prior pointer is more in the pointer field when defining a doubly linked list, the insert operation becomes complex, but the basic operation is not difficult to understand. Just remember that when dealing with the relationship between the precursor and the successor pointer and the insertion node, you should always grasp the "orderly principle", even if the insertion node and two existing nodes to form a triangle, you should first process the "up" pointer, and then process the "down" pointer. The following code describes its process:

pinsert->prior=p;
pinsert->next=p->next;
p->next->prior=pinsert;
p->next=pinsert;    

3. Delete operations for doubly linked lists
Once you understand the insert operation for a doubly linked list, the delete operation is easy to understand. The following code describes its process:

    p->prior->next=p->next;
    p->next->prior=p->prior;
    Free (p);

The other operation of the doubly linked list is similar to a single linked list, which is not described here, and the complete code is as follows:

#include <stdio.h> #include <stdlib.h> #include <time.h> #define OK 1 #define ERROR 0 #define TRUE 1 #defin
e FALSE 0 typedef int status;
typedef int ELEMTYPE;
    typedef struct node{elemtype data;
    struct node * NEXT;
struct node * prior;
}node;

typedef struct NODE* dlinklist;

Status visit (elemtype c) {printf ("%d", c);}
    /* Bidirectional list initialization */status Initdlinklist (dlinklist * head,dlinklist * tail) {(*head) = (dlinklist) malloc (sizeof (node));
    (*tail) = (dlinklist) malloc (sizeof (node)); if (! ( *head) | |!
        (*tail))
    return ERROR;
    /* This step is critical */(*head)->prior=null;
    (*tail)->next=null;
    /* When the list is empty let the head point to the tail */(*head)->next= (*tail);
(*tail)->prior= (*head);
    }/* Determines if NULL */Status Emptylinklist (dlinklist head,dlinklist tail) {if (head->next==tail) return TRUE;
else return FALSE; }/* Tail interpolation to create a linked list */status Createdlinklisttail (dlinklist head,dlinklist tail,elemtype data) {dlinklist Pmove=tail,pinsert
    ; Pinsert= (DlinklIST) malloc (sizeof (node));
    if (!pinsert) return ERROR;
    pinsert->data=data;
    pinsert->next=null;
    pinsert->prior=null;
    tail->prior->next=pinsert;
    pinsert->prior=tail->prior;
    pinsert->next=tail;
tail->prior=pinsert; }/* Header interpolation to create a linked list */status Createdlinklisthead (dlinklist head,dlinklist tail,elemtype data) {dlinklist pmove=head,qmove=t
    Ail,pinsert;
    Pinsert= (dlinklist) malloc (sizeof (node));
    if (!pinsert) return ERROR;
        else{pinsert->data=data;
        pinsert->prior=pmove;
        pinsert->next=pmove->next;
        pmove->next->prior=pinsert;
    pmove->next=pinsert;
    }}/* Positive order Print List */status Traverselist (dlinklist head,dlinklist tail) {/*dlinklist pmove=head->next;
        while (Pmove!=tail) {printf ("%d", pmove->data);
    pmove=pmove->next;
    } printf ("\ n");
    return ok;*/dlinklist pmove=head->next; while (Pmove!=tail) {VisiT (Pmove->data);
    pmove=pmove->next;
} printf ("\ n"); }/* Returns the bit order of the first element with the value data */Status Locateelem (dlinklist head,dlinklist tail,elemtype data) {dlinklist Pmove=head->next
    ;
    int Pos=1;
        while (Pmove&&pmove->data!=data) {pmove=pmove->next;
    pos++;
} return POS;
    }/* Returns the table length */Status Listlength (dlinklist head,dlinklist tail) {dlinklist pmove=head->next;
    int length=0;
        while (Pmove!=tail) {pmove=pmove->next;
    length++;
} return length;
    }/* Reverse Print List */status inverse (dlinklist head,dlinklist tail) {dlinklist pmove=tail->prior;
        while (Pmove!=head) {visit (pmove->data);
    pmove=pmove->prior;
} printf ("\ n");
    }/* Remove the element from POS in the list and return it with data */Status Deleteelem (dlinklist head,dlinklist tail,int pos,elemtype *data) {int i=1;
    Dlinklist pmove=head->next;
        while (Pmove&&i<pos) {pmove=pmove->next;
    i++; } if (!pmove| | I>pos) {printf ("illegal input data \ n");
    return ERROR;
        } else{*data=pmove->data;
        pmove->next->prior=pmove->prior;
        pmove->prior->next=pmove->next;
    Free (pmove);
    }/* Inserts elements at the end of the list */Status Inserttail (dlinklist head,dlinklist tail,elemtype data) {dlinklist Pinsert;
    Pinsert= (dlinklist) malloc (sizeof (node));
    pinsert->data=data;
    pinsert->next=null;
    pinsert->prior=null;
    tail->prior->next=pinsert;
    pinsert->prior=tail->prior;
    pinsert->next=tail;
    tail->prior=pinsert;
return OK;
    } int main (void) {dlinklist head,tail;
    int i=0;
    Elemtype data=0;
    Initdlinklist (&head,&tail);
    if (Emptylinklist (head,tail)) printf ("The list is empty \ n");
    else printf ("The list is not empty \ n"); 
    printf ("header interpolation to create a linked list \ n");
    for (i=0;i<10;i++) {createdlinklisthead (head,tail,i);

    } traverselist (Head,tail); for (i=0;i<10;i++) {printf ("The table has a value of%dThe position of the vegetarian is ", i);
    printf ("%d bits \ n", Locateelem (Head,tail,i));
    } printf ("Table length is%d\n", Listlength (Head,tail));
    printf ("Reverse Print List");
    Inverse (head,tail);
        for (i=0;i<10;i++) {Deleteelem (head,tail,1,&data);
    printf ("deleted element is%d\n", data);
    } traverselist (Head,tail);
    if (Emptylinklist (head,tail)) printf ("The list is empty \ n");
        else printf ("The list is not empty \ n");
    printf ("tail interpolation method to create a linked list \ n");
        for (i=0;i<10;i++) {//inserttail (head,tail,i);
    Createdlinklisttail (Head,tail,i);
    } traverselist (Head,tail);
    printf ("Reverse Print List");
Inverse (head,tail); }

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.