Doubly linked list and its usage

Source: Internet
Author: User

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 as follows:

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 the first element datavoid 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 the element of the 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->da                TA) {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.


Doubly linked list and its usage

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.