C++ bidirectional circular list usage example
- 2020-04-02 03:10:41
- OfStack
This article illustrates the use of C++ bidirectional looping lists as an example. Share with you for your reference. The details are as follows:
#include <iostream>
using namespace std;
//Structs construct the pointer and data fields of a linked list
struct ChainNode
{
int data; //Node data
ChainNode *left; //The precursor pointer to the node
ChainNode *right; //The subsequent pointer to the node
};
//////////// create n Two - way circular linked lists And returns the chain header pointer /////////
ChainNode* CreateNode(int n)
{
ChainNode *head = NULL; //Chain header node
ChainNode *pCur=NULL,*pNew=NULL; //Current node, new node
//The precursor and successor nodes of the initialized header are NULL
if (n<1) //No node returns the header node
{
return head;
}
//Creates the header node and points the left and right Pointers to the null
head = new ChainNode;
head->left = NULL;
head->right = NULL;
head->data = 0;
pCur = head;
//In order to prevent the confusion caused by pointer mutual pointing, the pCur node is used to save the head node to indicate that the current pointer has moved to the head pointer
//Create n nodes and join them into a 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 to the header pointer points to the new node
pNew->left = pCur; //The left pointer to the new node executes the header node
pNew->right = NULL; //Used for swapping final and header Pointers
pCur = pNew; //The pointer moves down
}
//Finally, the left pointer of the header pointer points to the last node,
//The last node has a pointer to the head pointer, constitute a loop
head->left = pCur;
pCur->right = head;
return head;
}
////////////// The output Chain header node///////////////////////
void OutList(ChainNode *head) //Parameter is the head pointer from the head pointer
{
cout<<" The output of linked list elements is as follows: "<<endl;
ChainNode *pCur = head->right;
//Start the output by restarting the first node
//If there is no point to an empty node, the list does not end with 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 list
}
cout<<pCur->data<<endl;
//Enter the last element, and its right pointer executes the head
}
/////// add n nodes after the bidirectional loop 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;
//The current node moves down to the last node
}
//Add n new nodes and insert the list
for (int i=0; i<n; i++)
{
pNew = new ChainNode;
cout<<" Enter the node element to be added: ";
cin>>pNew->data;
pCur->right = pNew; //The right pointer to the header pointer points to the new node
pNew->left = pCur; //The left pointer to the new node executes the header node
pNew->right = NULL; //Used for swapping final and header Pointers
pCur = pNew; //The pointer moves down
}
//Finally, the left pointer of the header pointer points to the last node,
//The last node has a pointer to the head pointer, constitute a loop
head->left = pCur;
pCur->right = head;
return head;
}
///// Remove a node from a bidirectional circular list ///////
ChainNode* DeleteNode(ChainNode* head, unsigned num)
//Delete the num node
{
ChainNode *pNew,*pCur,*temp;
//New add node and current node , Temporary switching node
pCur = head;
int ncount = 0;
//Move to the num-1 node
while (1)
{
ncount++;
pCur = pCur->right; //The current node moves down
if (num == ncount)
{
break; //PCur is still pointing to the NTH node
}
}
//The right pointer to the previous node of the current node points to the next node of the current node
//The left pointer of the next node of the current 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 them
cout<<" Enter the number of list nodes to create :";
cin>>num;
ChainNode *head = CreateNode(num);
OutList(head);
//Add n nodes to the list
int addnum;
cout<<" Enter the number of nodes to add :";
cin>>addnum;
AddNode(head, addnum);
OutList(head);
//Deletes the del th element of the list
int del;
cout<<" Enter the node at which location you want to delete :";
cin>>del;
DeleteNode (head, del);
OutList(head);
system("pause");
return 0;
}
Hope that the article described in the C++ programming to help you.