Algorithm and data structure [Java]: circular list

Keywords: Java

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 Java code:

package com.circularLinkedList;

//Circular list class
public class CircularLinkedList {
	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
	
	public CircularLinkedList(){
		head = null;
		tail = null;
		length = 0;
	}
	
	//Judge whether the list is empty
	int isEmpty(){
		return head==null ? 1: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 1;
		return 0;
	}
	
	//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 == null){
				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==null){
				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==null && 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=null;
				//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==null)
				return -1;
			//Deleted nodes and deleted values
			Node deletedNode=null;
			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 = null;
			}
			//Otherwise, it will be deleted normally
			else{
				deletedNode = head;
				tail.next = head.next;
				head = head.next;
			}
			length--;
			deletedValue = deletedNode.value;
			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==null)
				return -1;
			//Deleted nodes and deleted values
			Node deletedNode=null;
			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 = null;
			}
			//Otherwise, it will be deleted normally
			else{
				int cnt=0;
				Node aheadOfDelete = null;
				for(aheadOfDelete=head; aheadOfDelete.next!=tail; aheadOfDelete=aheadOfDelete.next);
				deletedNode = aheadOfDelete.next;
				aheadOfDelete.next = head;
				tail = aheadOfDelete;	
			}
			length--;
			
			deletedValue = deletedNode.value;
			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==null)
				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=null;
				//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--;
				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=null;
			System.out.print("CircularLinkedList: [");
			for (now=head,cnt=1; cnt<=length; cnt++,now=now.next)
				System.out.print(now.value + " ");
			
			System.out.print("]\n");	
			if (isEmpty()==0) {
				String str = String.format("\t\ttail->next: %d\tlength: %d %d\n", tail.next.value, length, isEmpty());
				System.out.print(str);	
			}
			else 
				System.out.print("\t\tEmpty CircularLinkedList\n");
		}
	
	static public void main(String[] argv) {
		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();
		System.out.println("deletedValue: " + list.deleteNode(9));list.printSelf();
		//list.deleteFromHead();list.printSelf();
		//System.out.println("deleting!");
	}
}


class Node{
	int value;	//Node data
	Node next;	//Reference to next node
	
	//Constructor
	public Node(int aValue){
		value = aValue;
		next = null;
	}
	public Node(int aValue, Node aNext){
		value = aValue;
		next = aNext;
	}
}; 

 

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

Posted by smith.james0 on Sun, 26 Jan 2020 11:03:00 -0800