#include <stdio.h>#include<stdlib.h>typedefstructnode*Dnode;structNode {intdata; Dnode Prior; //Previous data addressDnode Next;//Back Data address}; //Create a doubly linked listvoidCreatnode (Dnode *head) {Dnode s; //new node Pointer Chare; (*head) = (Dnode)malloc(sizeof(structnode));//Head knot Point(*head)->prior = (*head);//both the precursor and the rear drive of the initial head node point to themselves.(*head)->next = (*head); printf ("input data \ n"); scanf ("%c", &e); while(e!='\ n') {s= (Dnode)malloc(sizeof(structnode));//new node allocation spaceS->data =e; S->prior = (*head);//the prior of the new node is connected to a previous junction .S->next = (*head)->next;//next junction of new node(*head)->next->prior = s;//the prior of the latter node is connected to a new node .(*head)->next = s;//Next link new node in front of new nodesscanf"%c", &e); }}//traverse the output backwardsvoidPrintList1 (Dnode L) {Dnode p; P=L; P= p->Next; while(P! =L) {printf ("%c", p->data); P= p->Next; } printf ("\ n");}//traverse the output forwardvoidPrintList2 (Dnode L) {Dnode p; P= l->Prior; while(P! =L) {printf ("%c", p->data); P= p->Prior; } printf ("\ n");}//find the address of data in section IDnode findposition (Dnode L,inti) {intj =0; Dnode P=L; while(P->next! = L&&j <i) {p= p->Next; J++; } returnp;}//Insertvoidinsertlist (Dnode L) {Dnode s,p; //S is the new node p is the previous junction of the new nodal point inti; Chare; printf ("insert in the first place: \ n"); scanf ("%d", &i); GetChar (); printf ("What data is inserted: \ n"); scanf ("%c", &e); P= Findposition (L, I-1);//new node Previous addresss = (Dnode)malloc(sizeof(structnode));//Request new node spaceS->data =e; S->prior = p;//the prior of the new node is connected to one node.S->next = p->next;//Next link on the new nodeP->next->prior = s;//The prior of the nodes after the new node is connected to the new junction .P->next = s;//Next link to new node in front of new nodes}//Delete voiddeletelist (Dnode L) {Dnode s,p; //S is the new node P is the node to delete inti; printf ("Delete the data in the first place: \ n"); scanf ("%d", &i); P= Findposition (L, i);//to delete the address of a nodeP->prior->next = p->next;//to delete the node of the previous node of next, connect the node after the node to be deletedP->next->prior = p->prior;//To delete the prior of the last node of the node, connect the previous node of the node to be deleted. Free(P);}intMain () {Dnode list; Creatnode (&list); //PrintList1 (list);PrintList2 (list); Insertlist (list); PrintList2 (list); DeleteList (list); PrintList2 (list);}
The basic operation of the doubly linked list C language