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

1. Definition: circular linked list is a kind of linked list which has no head and tail, but in order to provide access to the linked list and facilitate the operation of the linked list, "head" and "tail" are defined
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;

 //Node class of linked list
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
class CircularLinkedList{
	public:
		Node* head;		//Head node of linked list
						//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
	CircularLinkedList(){
		head = 0;
		tail = 0;
		length = 0;
	}

	//Judge whether the list is empty
	int isEmpty(){
		return head==0;
	}
	

	//Determine whether the linked list contains an element
	int contains(int value){
		//Search by traversing the linked list
		for (Node* now=head; now->next!=head; now=now->next)
			if(now->value == value)
				return true;
		return false;
	}
	
	//Add node to head
	void addToHead(int value){
		Node* tmp = new Node(value);
		//If the list is empty, update the head and tail
		if(head == 0){
			tmp->next = tmp;
			head = tail = tmp;
		}
		//Otherwise, only the head will be updated
		else{
			tmp->next = head;
			tail->next = tmp;	
			head = tmp;			
		}
		length++;
	}


	//Add node to tail
	void addToTail(int value){
		Node* tmp = new Node(value);
		//If the list is empty, update the head and tail
		if (head==0){
			tmp->next = tmp;
			head = tail = tmp;
		}
		//Otherwise, only tail will be updated
		else {
			tmp->next = head;
			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
	void addNode(int value, int index){
		//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)
		if (head==0 && index==1){
			Node* tmp = new Node(value);
			tmp->next = tmp;
			head = tail = tmp;
		}	
		//If you add a node to the chain header, call addToHead
		else if (index==1){
			addToHead(value);
		}
		//If you append a node to the end of the list, call addToTail
		else if (index==length+1)
			addToTail(value);
		//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
			Node* aheadOfAdd=0;
			//Loop to find the previous node to add a node
			for (cnt=1,aheadOfAdd=head; cnt+1<index; aheadOfAdd=aheadOfAdd->next,cnt++);
			//Add node
			tmp->next = aheadOfAdd->next;
			aheadOfAdd->next = tmp;
			length++;
		}
	}

	//Delete node from head
	int deleteFromHead(){
		//The linked list is empty. It can't be deleted any more. Return directly
		if(head==0)
			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
		if (head==tail){
			deletedNode = head;
			head = tail = 0;
		}
		//Otherwise, it will be deleted normally
		else{
			deletedNode = head;
			tail->next = head->next;
			head = head->next;
		}
		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
		if(head==0)
			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
		if (head==tail){
			deletedNode = tail;
			head = tail = 0;
		}
		//Otherwise, it will be deleted normally
		else{
			int cnt=0;
			Node* aheadOfDelete = 0;
			for(aheadOfDelete=head; aheadOfDelete->next!=tail; aheadOfDelete=aheadOfDelete->next);
			deletedNode = aheadOfDelete->next;
			aheadOfDelete->next = head;
			tail = aheadOfDelete;	
		}
		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)
			return deleteFromHead();
		//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
			Node* aheadOfDelete=0;
			//Loop to find the node
			for(cnt=1,aheadOfDelete=head; cnt+1<index; aheadOfDelete=aheadOfDelete->next,cnt++);
			//Nodes to delete and corresponding values
			Node* deletedNode = aheadOfDelete->next;
			int deletedValue = deletedNode->value;
			//Delete this node
			aheadOfDelete->next = deletedNode->next;
			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("CircularLinkedList: [");
		for (now=head,cnt=1; cnt<=length; cnt++,now=now->next)
			printf("%d, ", now->value);
		
		printf("]\n");	
		if (!isEmpty())
			printf("\t\ttail->next: %d\tlength: %d %d\n", tail->next->value, length, isEmpty());
		else 
			printf("\t\tEmpty CircularLinkedList\n");
	}
};

//Test function
int main(){
	CircularLinkedList* list = new CircularLinkedList();
	list->addToHead(123);list->printSelf();
	list->addToHead(124);list->printSelf();
	list->addToHead(125);list->printSelf();
	list->addToTail(1);list->printSelf();
	list->addToTail(2);list->printSelf();
	list->addToTail(3);list->printSelf();
	list->addNode(10000,1);list->printSelf();
	list->addNode(10001,2);list->printSelf();
	list->addNode(10005,9);list->printSelf();
	printf("deletedValue: %d\n", list->deleteNode(9));list->printSelf();
	//list->deleteFromHead();list->printSelf();
	printf("deleting!\n");
	//while(!list->isEmpty()){
	//	list->deleteFromTail();list->printSelf();
	//}
}

 

79 original articles published, 53 praised, 40000 visitors+
Private letter follow

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