# Algorithm and data structure [C + +]: circular list

2. Circular list can be realized by simple addition and modification on one-way list, but it is realized separately here.
3. For example, when scheduling tasks in the operating system, tasks with the same priority will get the same CPU usage right at the same time. After one task occupies the CPU, it is necessary to give up the CPU to the next task. In this way, the circular list can be used.

Here is the C + + Code:

```#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;

class Node{
public:
int value;	//Node data
Node* next;	//Reference to next node

//Constructor
Node(int aValue, Node* aNext=0){
value = aValue;
next = aNext;
}
};

//Circular list class
public:
//Although there is no head and tail in the theory of circular list, but the list needs an entry, and in order to modify the list conveniently, there are assumed head and tail
Node* tail;		//Tail node of linked list
int length;		//Length of linked list

//Constructor
tail = 0;
length = 0;
}

//Judge whether the list is empty
int isEmpty(){
}

//Determine whether the linked list contains an element
int contains(int value){
//Search by traversing the linked list
if(now->value == value)
return true;
return false;
}

Node* tmp = new Node(value);
//If the list is empty, update the head and tail
tmp->next = tmp;
}
//Otherwise, only the head will be updated
else{
tail->next = tmp;
}
length++;
}

Node* tmp = new Node(value);
//If the list is empty, update the head and tail
tmp->next = tmp;
}
//Otherwise, only tail will be updated
else {
tail->next = tmp;
tail = tmp;
}
length++;
}

//Add a node to the specified subscript location starting from the beginning of the node. The subscript starts at 1 and ends at length
//Illegal subscript, direct return
if (index<=0 || index>length+1)
return ;

//If the list is empty, add a node to the location (addToHead should be called directly here)
Node* tmp = new Node(value);
tmp->next = tmp;
}
else if (index==1){
}
//If you append a node to the end of the list, call addToTail
else if (index==length+1)
//Otherwise, add nodes to the middle of the list
else{
//Counter for counting to
int cnt=0;
//Newly created node
Node* tmp = new Node(value);
//To add the previous node of a node
//Loop to find the previous node to add a node
length++;
}
}

//The linked list is empty. It can't be deleted any more. Return directly
return -1;
//Deleted nodes and deleted values
Node* deletedNode=0;
int deletedValue=0;
//If there is only one node, delete it directly, and update the head and tail to be empty
}
//Otherwise, it will be deleted normally
else{
}
length--;
deletedValue = deletedNode->value;
delete deletedNode;
return deletedValue;
}

//Delete a node from the end of the list
int deleteFromTail(){
//The linked list is empty. It can't be deleted any more. Return directly
return -1;
//Deleted nodes and deleted values
Node* deletedNode=0;
int deletedValue=0;
//If there is only one node, delete it directly, and update the head and tail to be empty
deletedNode = tail;
}
//Otherwise, it will be deleted normally
else{
int cnt=0;
}
length--;

deletedValue = deletedNode->value;
delete deletedNode;
return deletedValue;
}

//Delete node according to given subscript
int deleteNode(int index){
//If the subscript is illegal or the list is empty, return directly
if (index<0 || index>length || head==0)
return -1;

//To delete the first node, call deleteFromHead
if (index==1)
//The linked list has only one node, but the subscript is not 1. It is returned directly
else if(head == tail && index!=-1)
return -1;
//If you want to delete the tail node, call deleteFromTail directly
else if(index==length)
return deleteFromTail();
//Other circumstances
else{
//Counter, used to count the previous node to find the node to delete
int cnt=0;
//The previous node of the node to delete
//Loop to find the node
//Nodes to delete and corresponding values
int deletedValue = deletedNode->value;
//Delete this node
length--;
delete deletedNode;
return deletedValue;
}
}

//Print the contents of the linked list. Because it is a circular linked list, print the value of the next node according to tail - > next, and verify whether the circular structure is correct
void printSelf(){
int cnt=0;
Node* now=0;
printf("%d, ", now->value);

printf("]\n");
if (!isEmpty())
printf("\t\ttail->next: %d\tlength: %d %d\n", tail->next->value, length, isEmpty());
else
}
};

//Test function
int main(){
printf("deletedValue: %d\n", list->deleteNode(9));list->printSelf();
printf("deleting!\n");
//while(!list->isEmpty()){
//	list->deleteFromTail();list->printSelf();
//}
}

```  79 original articles published, 53 praised, 40000 visitors+

Posted by jharbin on Sun, 26 Jan 2020 09:22:03 -0800