C language implementation of two-way linked list delete nodes, insert nodes, two-way output and other operations

Source: Internet
Author: User

#include <cstdio> #include <cstdlib>typedef struct doublelinkedlist{int data;    struct Doublelinkedlist *pre; struct doublelinkedlist *next;}    dlinkedlist_node;//establish a linked list dlinkedlist_node* Createdlink () {Dlinkedlist_node *head,*p,*s;    int x;    Head = (dlinkedlist_node*) malloc (sizeof (Dlinkedlist_node));    p = head;        while (1) {printf ("Please input the data: \ n");        scanf ("%d", &x);            if (x! = 65535) {s = (dlinkedlist_node*) malloc (sizeof (Dlinkedlist_node));            s->data = x;            s-> pre = P;            P->next = s;        P=s;                } else {printf ("\ n data input end \ n");            Break    }} p->next = NULL;    Head = Head->next;    Head->pre = NULL; return head;}    Sequential, reverse-order Print List void Printdlink (Dlinkedlist_node *head) {Dlinkedlist_node *p,*s;    p = head;    printf ("Positive sequence output bidirectional linked list: \ n");        while (p) {printf ("%d", p->data);  s = p;      p = p->next;    } printf ("\ n Reverse output bidirectional list: \ n");        while (s) {printf ("%d", s->data);    s = s->pre; } printf ("\ nthe \ n");}    Delete a node dlinkedlist_node* deletedlinkedlist_node (Dlinkedlist_node *head,int i) {Dlinkedlist_node *p;    p = head;        if (P->data = = i) {head = p->next;        Head->pre = NULL;        Free (p);    return head;        } while (p) {if (P->data = = i) {P->pre->next = p->next;        P->next->pre = p->pre;        Free (p);        return head;    } p = p->next;    } printf ("The data you want to delete is not found \ n"); return head;}    Insert a node dlinkedlist_node* insertdlinkedlist_node (Dlinkedlist_node *head,int i) {Dlinkedlist_node *p,*temp;    p = head;    temp = (dlinkedlist_node*) malloc (sizeof (Dlinkedlist_node));    Temp->data = i;        if (I < p->data)//smaller than the head node data, inserted into the list head {head = temp;        Head->next = p;//Here P is the original head head->pre = NULL; P->pRe = head;//here P is the original head return head;    } while (P! = NULL && i > P->data)//Find the appropriate insertion position {p = p->next;        if (I < p->data)//Find the appropriate insertion position in the middle of the list {temp->next = p;        Temp->pre = p->pre;        P->pre->next = temp;        P->pre = temp;    return head;  } else//did not find a suitable location, only inserting data into the list trailing {p->next = temp;        Traverse to the tail of the list, P==null temp->pre = p;        Temp->next = NULL;    return head;    }}int Main () {Dlinkedlist_node *head;    Head = Createdlink ();    Printdlink (head);    Head = Insertdlinkedlist_node (head,1012);    Head = Deletedlinkedlist_node (head,1991); Printdlink (head);} /***************************** operation results are as follows: Please input the data:1991please input the Data:1992please input the data : 2013please Input The Data:2014please input the Data:512please input the Data:420please input the data:65535 data input end positive sequence output doubly linked list : 1991 1992 2013 2014 512 420 reverse output doubly linked list: 420 512 2014 2013 1992 1991 positive sequence output doubly linked list: 1012 1992 2013 2014 512 420 reverse output doubly linked list: 420 512 2014 2013 1992 1012******************************/ 

C language implements two-way linked list delete nodes, insert nodes, bidirectional output, and so on

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.