#include <stdio.h> #include <stdlib.h> typedef int ELEMTYPE;TYPEDEF struct Doublelinkedlist {Elemtyp e data; struct Doublelinkedlist *pre; struct Doublelinkedlist *next; }dlinkedlist_node; Establish linked list dlinkedlist_node* Create_dlink () {Dlinkedlist_node *head,*p,*s; int x; Head = (dlinkedlist_node*) malloc (sizeof (Dlinkedlist_node)); p = head; while (1) {printf ("Please enter the node to be saved, end with 0 input: \ n"); scanf ("%d", &x); if (x! = 0) {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 Print_dlink (Dlinkedlist_node *head) {Dlinkedlist_node *p,*s; p = head; printf ("In the positive order output doubly linked listNode: \ n "); while (p) {printf ("%d", p->data); s = p; p = p->next; } printf ("\ n reverse output a node in a doubly linked list: \ n"); while (s) {printf ("%d", s->data); s = s->pre; } printf ("\ n \ n"); }//Delete node of the specified element dlinkedlist_node* delete_dlinkedlist_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 ("Sorry for the wrong input data!") , did not find the data you want to delete \ n "); return head; }//Insert a node dlinkedlist_node* insert_dlinkedlist_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 = Create_dlink (); Print_dlink (head); return 0;}
Data structure---C-language implementation of two-way linked list operations