Understanding the implementation of circular queue

  • In the previous section, we found that multiple queue in and queue out operations will cause a "false overflow" phenomenon that there is storage space but can not enter the queue. The reason for this is that the storage unit of the sequence queue does not have a repeated storage mechanism. The solution is that if the data is stored all the way to the end of the array, the next storage location will be folded back to the beginning of the array . In this way, the end of the array is connected with the beginning of the array, so although the physical structure of the array is "line", its logical structure has become "ring".
  • Look at the code implementation of circular queue
public class MyCircleQueue implements IQueue<Integer> {

	private int[] nums = new int[6];
	/*
	 * There is a problem to be solved here, that is, to judge whether the conditions of team full and team empty have become front==rear, which is obviously not possible;
	 * How to solve it?
	 * Make the queue save one less and only length - 1 element at most, so that;
	 * Judge whether the condition of queue empty is front==rear,
	 * The condition of judging team full is front=(rear+1)%length;
	 */
	private int front = 0;//Team first pointer
	private int rear = 0;//Tail pointer
	private int length = nums.length;
	
	@Override
	public void clear() {
		// TODO Auto-generated method stub
		front = rear = 0;
		nums = new int[6];
	}
	
	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		if(front == rear) {
			return true;
		}
		return false;
	}
	
	@Override
	/*
	 * Return the number of elements stored in the queue
	 */
	public int length() {
		// TODO Auto-generated method stub
		//Direct real front before no cycle
		//Rear + length front
		//merge
		return (rear+length-front)%length;
	}
	
	@Override
	public Integer peek() throws Exception {
		// TODO Auto-generated method stub
		if(isEmpty()) {
			throw new Exception("The team is empty.");
		}
		return nums[front];
	}

	@Override
	public void offer(Integer t) throws Exception {
		// TODO Auto-generated method stub
		if((rear + 1) % length == front) {
			throw new Exception("The team is full.");
		}
		nums[rear] = t;
		rear = (rear + 1) % length;
	}

	@Override
	public Integer poll() throws Exception {
		// TODO Auto-generated method stub
		if(isEmpty()) {
			throw new Exception("The team is empty.");
		}
		Integer o = nums[front];
		front = (front + 1) % length;
		return o;
	}

	@Override
	public void display() {
		// TODO Auto-generated method stub
		if(isEmpty()) {
			System.out.println("The team is empty.");
		} else {
			for(int i = front; i < rear; i = (i + 1)%length) {
				System.out.print(nums[i] + " ");
			}
		}
	}

}
  • test
public class CircleQueueTest {

	public static void main(String[] args) throws Exception {
		MyCircleQueue circleQueue = new MyCircleQueue();
		circleQueue.offer(1);
		circleQueue.offer(2);
		circleQueue.offer(3);
		circleQueue.offer(4);
		circleQueue.offer(5);
		circleQueue.display();
		System.out.println();
		System.out.println("----------");
		System.out.println(circleQueue.length());
		System.out.println(circleQueue.peek());
		System.out.println(circleQueue.poll());
		System.out.println(circleQueue.length());
		circleQueue.display();
		
	}
}

test result

Keywords: less

Added by HaLo2FrEeEk on Mon, 25 Nov 2019 21:15:24 +0200