C + + bidirectional cyclic list usages _c language

Source: Internet
Author: User

The examples in this article describe the use of C + + bidirectional cyclic lists. Share to everyone for your reference. as follows:

/* bidirectional cycle list/#include <iostream> using namespace std;
The structure body constructs the link list the pointer domain and the data domain struct Chainnode {int data;//node data chainnode *left;//node's precursor pointer chainnode *right;//node successor pointer}; Create n bidirectional cyclic lists and return the list header pointer/////////chainnode* createnode (int n) {Chainnode *head = NULL;//Chain header node Chainnode *pcur =null,*pnew=null; 
 The current node, the predecessor and successor nodes of the new node//initialization head node are null if (n<1)//No node returns the header nodes {return head; 
 ///Create the head node and point the right and left pointer to the null header = new Chainnode;
 Head->left = NULL; 
 Head->right = NULL;
 Head->data = 0;
 Pcur = head; To prevent confusion caused by pointers, the Pcur node is used to save the head node also indicates that the current pointer moves to the head pointer//creates n nodes and connects to the linked list for (int i=0; i<n; i++) {pnew = new Chainnode;//Create
  A new node cout<< "Please enter data:";
  cin>>pnew->data; Pcur->right = pnew; The right pointer of the head pointer points to the new node pnew->left = Pcur; The left pointer execution head node of the new node pnew->right = NULL; For the final and head pointer exchange pcur = pnew; 
 The pointer moves down}//Finally, the left pointer of the head pointer points to the last node,//The last node has a pointer to the head pointer, which forms the loop head->left = Pcur;
 Pcur->right = head; 
return head; //////////////Output list HeaderNode///////////////////////void outlist (Chainnode *head)//parameter is head pointer start from scratch {cout<< "list element output is as follows:" <<endl;
  Chainnode *pcur = head->right;
   Heavy first node start output//no point to null node, then the list does not end the output list element while (Pcur->right!= head) {cout<<pcur->data<< "";
   Pcur = pcur->right;
  The current node points to the next node to traverse the linked list} cout<<pcur->data<<endl; 
 Enter the last element, and its right pointer executes head}///////adds n nodes after the bidirectional cyclic list//////chainnode* AddNode (chainnode* head, int n) {Chainnode *pnew,*pcur; 
 New Add node and current node pcur = head;
   Move to the most node while (Pcur->right!= head) {pcur = pcur->right;
  Move the current node down all the time to the last node///Add n nodes and insert a list for (int i=0; i<n; i++) {pnew = new Chainnode;
  cout<< "Enter the node element to add:";
  cin>>pnew->data; Pcur->right = pnew; The right pointer of the head pointer points to the new node pnew->left = Pcur; The left pointer execution head node of the new node pnew->right = NULL; For the final and head pointer exchange pcur = pnew; 
 The pointer moves down}//Finally, the left pointer of the head pointer points to the last node,//The last node has a pointer to the head pointer, which forms the loop head->left = Pcur; Pcur->right = HeaD 
return head; /////deletes a node in a two-way circular list///////chainnode* deletenode (chainnode* head, unsigned num)//delete num Node {chainnode *pnew,*pcur,*
 Temp 
 New Add node and current node, temporary swap node pcur = head; 
 int ncount = 0;
   Move to Num-1 node while (1) {ncount++; Pcur = pcur->right; The current node moves down if (num = = ncount) {break;//At this point pcur still points to num nodes}}//The right hand pointer of the previous node of the current node to the next node//current node of the current node
 The left pointer of one node points to the previous node of the current node to form a connection//finally delete the current node (pcur->left)->right = pcur->right;
 (pcur->right)->left = pcur->left;
 Delete pcur; 
return head;
 int main () {int num;
 Create NUM nodes and display cout<< "Enter the number of linked list nodes to create:";
 cin>>num;
 Chainnode *head = CreateNode (num); 
 Outlist (head);
 adding n nodes int addnum after the linked list;
 cout<< "Enter the number of nodes to add:"; 
 cin>>addnum;
 AddNode (head, addnum); 
 Outlist (head);
 Deletes the list del element int del;
 cout<< "Enter the node for the first few locations you want to delete:";
 cin>>del;
 Deletenode (Head, Del);
 Outlist (head);
 System ("pause");
return 0;

 }

I hope this article will help you with your C + + programming.

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.