Java day 16 - circular queue

Posted by SeanStar on Mon, 03 Jan 2022 17:02:18 +0100

The effect of division
Imagine putting a team of people in the playground runway, and the feeling of circulation comes out
In order to distinguish between empty queues and full queues, a space needs to be reserved It is equivalent to not allowing end to end connection You'd better draw a picture, otherwise it's easy to get into the pit
With chain structure, space allocation and recovery are done by the system, and with circular queue, it is controlled by itself Imagine that you are writing an operating system. From this code, you can feel the memory management
Integer queue code

package datastructure.list;

/**
 * @author goudiyuan
 */

public class CircularqQuene {

	//Length of declaration queue
	public final int TAOTAL_SPACE = 10;
	//Where to store elements
	int data[];
	//Queue header
	int head;
	//Queue tail
	int tail;

	//Construction method
	public CircularqQuene() {
		data = new int[TAOTAL_SPACE];
		head = 0;
		tail = 0;
	}

	//Join the team
	public void enQueue(int paraValue) {
		if ((tail + 1) % TAOTAL_SPACE == head) {
			System.out.println("The queue is full");
			return;
		}
		data[tail % TAOTAL_SPACE] = paraValue;
		tail++;
	}

	//Out of the team
	public int deQueue() {
		if (head == tail) {
			System.out.println("There are no more elements in the queue");
			return -1;
		}
		int resultValue = data[head % TAOTAL_SPACE];
		head++;
		return resultValue;
	}

	//Override the parent toString method
	public String toString() {
		String resultString = "";
		if (head == tail) {
			return "Empty queue";
		}
		for (int i = head; i < tail; i++) {
			resultString += data[i % TAOTAL_SPACE] + ",";
		}
		return resultString;
	}

	//Main function entry
	public static void main(String[] args) {
		CircularqQuene tempQueue = new CircularqQuene();
		System.out.println("After initialization, the queue is:" + tempQueue.toString());

		for (int i = 0; i < 5; i++) {
			tempQueue.enQueue(i + 1);
		}
		System.out.println("After joining the queue, the queue is:" + tempQueue.toString());

		int tempValue = tempQueue.deQueue();
		System.out.println("Will element" + tempValue + "After leaving the queue, the queue is" + tempQueue.toString());

		for (int i = 0; i < 6; i++) {
			tempQueue.enQueue(i + 10);
			System.out.println("After joining the queue, the queue is:" + tempQueue.toString());
		}

		for (int i = 0; i < 3; i++) {
			tempValue = tempQueue.deQueue();
			System.out.println("Will element" + tempValue + "After leaving the queue, the queue is" + tempQueue.toString());
		}

		for (int i = 0; i < 6; i++) {
			tempQueue.enQueue(i + 100);
			System.out.println("After joining the queue, the queue is:" + tempQueue.toString());

		}
	}

}

Operation results

After initialization, the queue is empty
After joining the team, the queue is: 1,2,3,4,5,
After element 1 is dequeued, the queue is 2,3,4,5,
After joining the team, the queue is: 2,3,4,5,10,
After joining the team, the queue is: 2,3,4,5,10,11,
After joining the team, the queue is: 2,3,4,5,10,11,12,
After joining the team, the queue is: 2,3,4,5,10,11,12,13,
After joining the team, the queue is: 2,3,4,5,10,11,12,13,14,
The queue is full
After joining the team, the queue is: 2,3,4,5,10,11,12,13,14,
After element 2 is dequeued, the queue is 3,4,5,10,11,12,13,14,
After element 3 is dequeued, the queue is 4,5,10,11,12,13,14,
After element 4 is dequeued, the queue is 5,10,11,12,13,14,
After joining the team, the queue is: 5,10,11,12,13,14100,
After joining the team, the queue is: 5,10,11,12,13,14100101,
After joining the team, the queue is: 5, 10, 11, 12, 13, 14100101102,
The queue is full
After joining the team, the queue is: 5, 10, 11, 12, 13, 14100101102,
The queue is full
After joining the team, the queue is: 5, 10, 11, 12, 13, 14100101102,
The queue is full
After joining the team, the queue is: 5, 10, 11, 12, 13, 14100101102,

Since string matching is used later, the data type of the queue is changed to char

code

package datastructure.list;

/**
 * @author goudiyuan
 */
public class CirclecharQueue {

	// queue length 
	public static final int TOTAL_SPACE = 10;
	// Where the queue holds data
	char[] data;
	// Head pointer
	int head;
	// Tail pointer
	int tail;

	// Construction method
	public CirclecharQueue() {
		data = new char[TOTAL_SPACE];
		head = 0;
		tail = 0;
	}

	// Join the team
	public void enqueue(char paraValue) {
		if ((tail + 1) % TOTAL_SPACE == head) {
			System.out.println("The queue is full.");
			return;
		}

		data[tail % TOTAL_SPACE] = paraValue;
		tail++;
	}

	// Out of the team
	public char dequeue() {
		if (head == tail) {
			System.out.println("There are no more elements in the queue");
			return '\0';
		}

		char resultValue = data[head % TOTAL_SPACE];

		head++;

		return resultValue;
	}

	// Override the parent toString method
	public String toString() {
		String resultString = "";

		if (head == tail) {
			return "empty";
		}

		for (int i = head; i < tail; i++) {
			resultString += data[i % TOTAL_SPACE] + ", ";
		}

		return resultString;
	}

	// Function entry
	public static void main(String args[]) {
		CirclecharQueue tempQueue = new CirclecharQueue();
		System.out.println("After initialization, the queue is " + tempQueue.toString());

		for (char i = '0'; i < '5'; i++) {
			tempQueue.enqueue(i);
		}
		System.out.println("After joining the queue, the queue is: " + tempQueue.toString());

		char tempValue = tempQueue.dequeue();
		System.out.println("Will element " + tempValue + "After leaving the queue, the queue is: " + tempQueue.toString());

		for (char i = 'a'; i < 'f'; i++) {
			tempQueue.enqueue(i);
			System.out.println("After joining the queue, the queue is: " + tempQueue.toString());
		}

		for (int i = 0; i < 3; i++) {
			tempValue = tempQueue.dequeue();
			System.out.println("Will element " + tempValue + "After leaving the queue, the queue is: " + tempQueue.toString());
		}

		for (char i = 'A'; i < 'F'; i++) {
			tempQueue.enqueue(i);
			System.out.println("After joining the queue, the queue is: " + tempQueue.toString());
		}
	}

}

Operation results

After initialization, the queue is empty
After joining the queue, the queue is: 0, 1, 2, 3, 4
After the element 0 is dequeued, the queue is: 1, 2, 3, 4
After joining the team, the queue is: 1, 2, 3, 4, a
After joining the team, the queue is: 1, 2, 3, 4, a, b
After joining the team, the queue is: 1, 2, 3, 4, a, b, c
After joining the team, the queue is: 1, 2, 3, 4, a, b, c, d
After joining the team, the queue is: 1, 2, 3, 4, a, b, c, d, e
After element 1 is dequeued, the queue is: 2, 3, 4, a, b, c, d, e
After element 2 is dequeued, the queue is: 3, 4, a, b, c, d, e
After element 3 is dequeued, the queue is: 4, a, b, c, d, e
After joining the team, the queue is: 4, a, b, c, d, e, A
After joining the team, the queue is: 4, a, b, c, d, e, A, B
After joining the team, the queue is: 4, a, b, c, d, e, A, B, C
The queue is full
After joining the team, the queue is: 4, a, b, c, d, e, A, B, C
The queue is full
After joining the team, the queue is: 4, a, b, c, d, e, A, B, C

 

Topics: Java Back-end