Doubly linked list and its usage

Source: Internet
Author: User
Tags prev

definition of a doubly linked list

doubly linked list is also a kind of linked list, it has two nodes in each data node, pointing to its direct precursor and direct successor respectively. So we can easily access its precursor and successor elements from any node in a doubly linked list.

two, two-way linked list storage structure

A doubly linked list is also a chained storage structure, which differs from a single-linked list in that each data node has a pointer field that points to the precursor element . its storage structure is shown below:

when a doubly linked list has only one node, its storage structure is as follows:

 

three, two-way linked list implementation and Operation

Because in a doubly linked list, we can access its precursor elements and successor elements through any node, the time complexity is O (1), so the doubly linked list is very convenient, we usually build the list will also choose to build a doubly linked list.

Next, look at the implementation and operation of the doubly linked list:

#include <stdio.h> #include <stdlib.h> #include <malloc.h> typedef struct DOUBLE_LIST {int data;
    struct Double_list *prev;
struct Double_list *next;
}double_list;
    Double_list *createlist ()//Create a doubly linked list with n elements and enter the element {double_list *head, *p, *q;
    int n,x;
    Head = (double_list *) malloc (sizeof (double_list));
    Head->prev = head;
    Head->next = head;
    p = head;
    printf ("Enter the number of elements to create a doubly linked list: \ n");
    scanf ("%d", &n);
        for (int i=0;i<n;i++) {scanf ("%d", &x);
        Q = (Double_list *) malloc (sizeof (double_list));
        Q->data = x;
        P->next = q;
        Head->prev = q;
        Q->prev = p;
        Q->next = head;
    p = q;
} return head;
    }//traverse and output these elements void Printlist (Double_list *head) {double_list *p;
    p = head;
    p = p->next;
        while (P!=head) {printf ("%d", p->data);
    p = p->next;
} printf ("\ n"); }//Get the number of elements in the doubly linked list now int lengthlist(Double_list *head)
    {Double_list *p;
    p = head;
    p = p->next;
    int coun = 0;
        while (P!=head) {coun++;
    p = p->next;
} return Coun;
    }//Insert data before element I (Insertlist_f) (double_list *head, int i, int data) {Double_list *p = head, *q;
    p = p->next;
    i--;
    while (i--) p = p->next;
    Q = (Double_list *) malloc (sizeof (double_list));
    Q->data = data;
    (p->prev)->next = q;
    Q->prev = p->prev;
    Q->next = p;
P->prev = q;
    }//delete element of I position void deletelist_i (double_list *head, int i) {double_list *p = head;
    p = p->next;
    i--;
    while (i--) p = p->next;
    (p->prev)->next = p->next;
    (p->next)->prev = p->prev;
Free (p);
    }//delete the element with a value of x void Deletelist_x (double_list *head, int x) {Double_list *p = head, *q;
    p = p->next;
            while (P!=head) if (P->data = = x) {q = p->next; (P->prev)->next = p->next;
            (p->next)->prev = p->prev;
            Free (p);
        p = q;
} else P = p->next;
    }//Sort the doubly linked list void sortlist (Double_list *head)//Ascending {double_list *p = head, *q, *t;
    p = p->next; for (;p!=head;p=p->next) for (t = p->next;t!=head;t=t->next) {if (P->data > T-&gt
                ;d ata) {int a = p->data;
                P->data = t->data;
            T->data = A;
    }}} int main () {double_list *head;
    Head = CreateList ();
    Deletelist_x (head, 2);
    Sortlist (head);
    Printlist (head);
    Insertlist_f (Head, 2, 2);

    Printlist (head);
return 0;
 }

For the two-way linked list of the traversal, I only give the pre-sequence traversal of the code, no post-order traversal of the code, which is actually similar, but because the two-way list can be carried out in two direction of the traversal, which brings us a great convenience.


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.