Final review of data structure (ろく)--Doubly linked list

Source: Internet
Author: User

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

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.