622. Design Circular Queue
Design Circular Queue https://leetcode.com/problems/design-circular-queue/discuss/149420/Concise-Java-using-array/167623 https://www.youtube.com/watch?v=Ig34WPrgofI dequeue doesn’t change the value at the top, it just moves the front pointer to the next one , and len - -. 还是不太明白, 走个例子 Enqueue, dequeue 和 queue 中 的 push(), pop() 一样的 class MyCircularQueue { // leave the global var here so they are be accessed from all public classes int[] a; int front, rear = -1; // idk why set -1 int len = 0; /** Initialize your data structure here. Set the size of the queue to be k. */ public MyCircularQueue(int k) { a = new int[k]; } /** Insert an element into the circular queue. Return true if the operation is successful. */ public boolean enQueue(int value) { if(!isFull()){ len++; rear = (rear + 1) % a.length;// so when rear index is 2, and array is not full, we can move to index 0 a[rear] = value; return true; }else{ return false; } } /** Delete an element from the circular queue. Return true if the operation is successful. */ public boolean deQueue() { if(!isEmpty()){ // deque , move front pointer to the next one, don't actually delete the val front = (front + 1) % a.length;// not sure why % len--; return true; }else{ return false; } } /** Get the front item from the queue. */ public int Front() { if(isEmpty()){ return -1; }else{ return a[front]; } } /** Get the last item from the queue. */ public int Rear() { if(isEmpty()){ return -1; }else{ return a[rear]; } } /** Checks whether the circular queue is empty or not. */ public boolean isEmpty() { return len == 0; } /** Checks whether the circular queue is full or not. */ public boolean isFull() { return len == a.length; // try return len == k; } }
Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.
Your implementation should support following operations:
MyCircularQueue(k): Constructor, set the size of the queue to be k.Front: Get the front item from the queue. If the queue is empty, return -1.Rear: Get the last item from the queue. If the queue is empty, return -1.enQueue(value): Insert an element into the circular queue. Return true if the operation is successful.deQueue(): Delete an element from the circular queue. Return true if the operation is successful.isEmpty(): Checks whether the circular queue is empty or not.isFull(): Checks whether the circular queue is full or not.
Example:
MyCircularQueue circularQueue = new MycircularQueue(3); // set the size to be 3 circularQueue.enQueue(1); // return true circularQueue.enQueue(2); // return true circularQueue.enQueue(3); // return true circularQueue.enQueue(4); // return false, the queue is full circularQueue.Rear(); // return 3 circularQueue.isFull(); // return true circularQueue.deQueue(); // return true circularQueue.enQueue(4); // return true circularQueue.Rear(); // return 4
posted on 2018-08-28 20:53 猪猪🐷 阅读(209) 评论(0) 收藏 举报
浙公网安备 33010602011771号