1#include <iostream>2 using namespacestd;3typedefstructDulnode4 {5 intdata;6 structDulnode *next;//successor node7 structDulnode *prior;//precursor node8}dulnode,*dullinklist;9 Ten One voidCreat_dlink (Dullinklist &l)//To create a single-linked list by tail interpolation A { -L =NewDulnode; -l->next=null;//Create an empty list of lead nodes first . theDullinklist p=l;//Intermediate Variables - intN; -cout<<"1) How many numbers do you want to enter? :"; -Cin>>N; +cout<<"Please enter table data, separated by spaces:"; - for(inti =0; i<n;++i) + { ADullinklist s =NewDulnode;//Intermediate Variables atcin>>s->data;//assigns the value of the INPUT element to S -s->next=null;//after inserting a new node into the head node -p->next=s; -S->prior=p; -p=s; - } incout<<"Build Table Success \n2) The linked list is:"; - } to +Dulnode *getelem (Dullinklist L,intI//position pointer p for position I - { theDullinklist p =NewDulnode; *P=l->Next; $ intj=1;Panax Notoginseng while(p&&j<i) { -p = p->Next; the++J; + } A returnp; the } + - intInsert_dullink (Dullinklist &l,intIintE//Insert $ { $Dullinklist p=NewDulnode; -Dullinklist s=NewDulnode; -p=Getelem (l,i); the if(!p) - return-1;WuyiS->data =e; theS->next = p->next; P->next =s; -S->next->prior = s; S->prior =p; Wu return 0; - About } $ - intDelete_dullink (Dullinklist &l,intI//Delete - { -Dullinklist p=NewDulnode; Ap=Getelem (l,i); + if(!p) the return-1; -P->prior->next=p->Next; $P->next->prior=p->Prior; the Deletep; thecout<<"Delete succeeded \ n"; the return 0; the } - in voidPrint_dlink (Dullinklist L)//Output the { theDullinklist p=l->Next; About while(p) { thecout<<p->data<<" "; theP=p->Next; the } +cout<<Endl; - } the Bayi intMain () the { the dullinklist L; - intb; - Creat_dlink (L); the Print_dlink (L); thecout<<"3) Enter the position and value you want to insert:"; theCin>>a>>b; the Insert_dullink (l,a,b); -cout<<"after inserting the linked list is:"; the Print_dlink (L); the inti; thecout<<"4) Please enter the location to delete:";94Cin>>i; the Delete_dullink (l,i); thecout<<"after deleting the linked list is:"; the Print_dlink (L);98 return 0; About}
The compilation results are as follows:
Final review of data structure (ろく)--Doubly linked list