题目

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.

Implementation the MyCircularQueue class:

  • MyCircularQueue(k) Initializes the object with the size of the queue to be k.
  • int Front() Gets the front item from the queue. If the queue is empty, return -1.
  • int Rear() Gets the last item from the queue. If the queue is empty, return -1.
  • boolean enQueue(int value) Inserts an element into the circular queue. Return true if the operation is successful.
  • boolean deQueue() Deletes an element from the circular queue. Return true if the operation is successful.
  • boolean isEmpty() Checks whether the circular queue is empty or not.
  • boolean isFull() Checks whether the circular queue is full or not.

Example 1:

  1. Input
  2. ["MyCircularQueue", "enQueue", "enQueue", "enQueue", "enQueue", "Rear", "isFull", "deQueue", "enQueue", "Rear"]
  3. [[3], [1], [2], [3], [4], [], [], [], [4], []]
  4. Output
  5. [null, true, true, true, false, 3, true, true, true, 4]
  6. Explanation
  7. MyCircularQueue myCircularQueue = new MyCircularQueue(3);
  8. myCircularQueue.enQueue(1); // return True
  9. myCircularQueue.enQueue(2); // return True
  10. myCircularQueue.enQueue(3); // return True
  11. myCircularQueue.enQueue(4); // return False
  12. myCircularQueue.Rear(); // return 3
  13. myCircularQueue.isFull(); // return True
  14. myCircularQueue.deQueue(); // return True
  15. myCircularQueue.enQueue(4); // return True
  16. myCircularQueue.Rear(); // return 4

Constraints:

  • 1 <= k <= 1000
  • 0 <= value <= 1000
  • At most 3000 calls will be made to enQueue, deQueue, Front, Rear, isEmpty, and isFull.

Follow up: Could you solve the problem without using the built-in queue?


题意

实现一个循环队列。

思路

用数组很容易实现。


代码实现

Java

  1. class MyCircularQueue {
  2. private int size;
  3. private int head, tail;
  4. private int[] q;
  5. public MyCircularQueue(int k) {
  6. size = 0;
  7. head = tail = 0;
  8. q = new int[k];
  9. }
  10. public boolean enQueue(int value) {
  11. if (isFull()) return false;
  12. q[tail++] = value;
  13. if (tail == q.length) tail = 0;
  14. size++;
  15. return true;
  16. }
  17. public boolean deQueue() {
  18. if (isEmpty()) return false;
  19. head++;
  20. if (head == q.length) head = 0;
  21. size--;
  22. return true;
  23. }
  24. public int Front() {
  25. if (isEmpty()) return -1;
  26. return q[head];
  27. }
  28. public int Rear() {
  29. if (isEmpty()) return -1;
  30. int index = tail - 1 == -1 ? q.length - 1 : tail - 1;
  31. return q[index];
  32. }
  33. public boolean isEmpty() {
  34. return size == 0;
  35. }
  36. public boolean isFull() {
  37. return size == q.length;
  38. }
  39. }