Bidirectional loop Linked list (C + + implementation)

Source: Internet
Author: User

In this bidirectional circular list, the header node first does not have an element, and when the bidirectional loop list is empty:first->rlink=first->llink=first;

The following code realizes the insertion and deletion of the bidirectional circular linked list, and in the insert operation, the head interpolation method and the sequential insertion method are implemented.

//main.cpp//----------Establish a two-way circular link list-------#include<iostream>Using namespace Std;class dblist;class dblistnode{friend class DBList;Private: intData; Dblistnode*Llink,*Rlink;}; Class dblist{ Public: DBList ()//Using a constructor to create a circular linked list with a head node (no element stored){First=NewDblistnode (); First -Llink=First -Rlink=First };voidInsert1 (int);voidInsert2 (int);voiddel (int);voidShow ();Private: Dblistnode*First;};voidDBList:: Insert1(int x)//Insert element with head interpolation, note the order of insertion steps{Dblistnode*P=NewDblistnode (); P -Data=X P -Rlink=First -Rlink;//Insertion Step (1)P -Llink=First//Insertion Step (2)First -Rlink -Llink=P//Insertion Step (3)First -Rlink=P//Insertion Step (4)}voidDBList:: Insert2(int x)//Insert elements sequentially, note the order of insertion steps{Dblistnode*P=NewDblistnode (); P -Data=X Dblistnode*Q=First -Rlink;if(q==First//When the double-linked list is empty, insert it at the top of the chain{p -Rlink=First P -Llink=First First -Llink=P First -Rlink=P }Else{ while(q -Rlink!=First&&Q -Data<=x) {Q=Q -Rlink; }if(q -Data>X//Intermediate insertion{p -Rlink=Q//Insertion Step (1)P -Llink=Q -Llink;//Insertion Step (2)Q -Llink -Rlink=P//Insertion Step (3)Q -Llink=P//Insertion Step (4)}Else          //When the insertion position is at the end of the chain{p -Rlink=First P -Llink=Q First -Llink=P Q -Rlink=P }     }}voidDBList::d el(int x) {Dblistnode*Q=First -Rlink; while(q!=First&&Q -Data!=X//Starting from the first element to find{Q=Q -Rlink; }if(q!=First//Find x{Q -Llink -Rlink=Q -Rlink;//Perform a delete operationQ -Rlink -Llink=Q -Llink;  Delete q; }Else{cout<<"x cannot be found, cannot be deleted"<<Endl }}voidDBList:: Show() {Dblistnode*P=First -Rlink; cout<<"list element is:"; while(p!=First) {cout<<P -Data<<" "; P=P -Rlink; } cout<<Endl;}  int main () {dblist l1,l2; L1.Insert1 ( -); L1.Insert1 ( -); L1.Insert1 ( +); L1.Insert1 ( -); L1.Show (); cout<<"After removing element 40"<<Endl L1.Del +); L1.Show (); L2.Insert2 ( -); L2.Insert2 ( -); L2.Insert2 ( +); L2.Insert2 ( -); L2.Show (); cout<<"After removing element 40"<<Endl L2.Del +); L2.Show (); System"Pause");return 0;}

Bidirectional loop Linked list (C + + implementation)

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.