Contents
  1. 1. 题目
  2. 2. 思考思路
  3. 3. 解题过程
    1. 3.1. 解法1
      1. 3.1.1. 总结
    2. 3.2. 解法2
      1. 3.2.1. 总结

题目

设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。
循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
你的实现应该支持如下操作:

  • MyCircularQueue(k): 构造器,设置队列长度为 k 。
  • Front: 从队首获取元素。如果队列为空,返回 -1 。
  • Rear: 获取队尾元素。如果队列为空,返回 -1 。
  • enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
  • deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
  • isEmpty(): 检查循环队列是否为空。
  • isFull(): 检查循环队列是否已满。
    示例:
    MyCircularQueue circularQueue = new MycircularQueue(3); // 设置长度为 3
    circularQueue.enQueue(1); // 返回 true
    circularQueue.enQueue(2); // 返回 true
    circularQueue.enQueue(3); // 返回 true
    circularQueue.enQueue(4); // 返回 false,队列已满
    circularQueue.Rear(); // 返回 3
    circularQueue.isFull(); // 返回 true
    circularQueue.deQueue(); // 返回 true
    circularQueue.enQueue(4); // 返回 true
    circularQueue.Rear(); // 返回 4
    提示:
  • 所有的值都在 0 至 1000 的范围内;
  • 操作数将在 1 至 1000 的范围内;
  • 请不要使用内置的队列库。

原题目地址:https://leetcode-cn.com/problems/design-circular-queue/submissions/

思考思路

队列实现有2种选择,一种是数组实现,一种是链表实现。
这个题目长度是固定的,用数组实现更合适。

解题过程

解法1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class MyCircularQueue {
private int head;
private int tail;
private int size;
private int currentSize;
int[] queue;
//顺序队列

/** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k) {
this.size = k;
queue = new int[size];
head = 0;
tail = 0;
currentSize = 0;
}

/** Insert an element into the circular queue. Return true if the operation is successful. */
public boolean enQueue(int value) {
if(currentSize == size){
return false;
}
if(currentSize == 0){
head = (head+1)%size;
}
tail = (tail+1)%size;

queue[tail] = value;
currentSize++;
return true;

}
/** Delete an element from the circular queue. Return true if the operation is successful. */
public boolean deQueue() {
if(currentSize == 0){
return false;
}

currentSize--;
if(currentSize != 0){
head = (head + 1)%size;
}
return true;
}

/** Get the front item from the queue. */
public int Front() {
if(currentSize == 0){
return -1;
}
return queue[head];
}

/** Get the last item from the queue. */
public int Rear() {
if(currentSize== 0){
return -1;
}
return queue[tail];

}
/** Checks whether the circular queue is empty or not. */
public boolean isEmpty() {
if(currentSize == 0){
return true;
}
return false;

}

/** Checks whether the circular queue is full or not. */
public boolean isFull() {
if(currentSize == size){
return true;
}
return false;
}
}

总结

思路1,head头指针,tail尾指针,size数组整个大小,currentSize当前数组大小,用于判断边界条件。
head和tail都是真的有数据位,isFull和ieEmpty根据currentSize判断即可,rear直接去tail,font直接取头。
循环存值的方法,就是(当前值+1)%数组长度,保证数组到头,可以从头再来 。
难点在enqueue和dequeue
enqueue时,通过currentSize==size判断是不是队列满了,其实可以调用isFull,没有想到功能的复用。
正常情况,压数据入队列尾,数据存在tail+1的位置,currentSize+1
特殊情况,队列是空的情况,head,tail同时后移1位,这种处理后面发现会浪费一个空间,当时没想到。
dequeue,先判断是不是空队列
正常情况,head+1跟size取余
如果currentSize==0,则什么都不多,防止指针变成负值
总结
难点1,如果保证存储数组可以循环
难点2,队列是空的是,头尾指针的判断

解法2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class MyCircularQueue {
private Integer[] queue;
private int head;
private int tail;
/** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k) {
queue = new Integer[k];
head = 0;
tail = 0;
}

/** Insert an element into the circular queue. Return true if the operation is successful. */
public boolean enQueue(int value) {
if(isFull()){
return false;
}
queue[tail] = value;
tail = (tail + 1) % queue.length;
return true;
}

/** Delete an element from the circular queue. Return true if the operation is successful. */
public boolean deQueue() {
if(isEmpty()){
return false;
}
queue[head] = null;
head = (head + 1) % queue.length;
return true;
}

/** Get the front item from the queue. */
public int Front() {
if(isEmpty()){
return -1;
}
return queue[head];
}

/** Get the last item from the queue. */
public int Rear() {
if(isEmpty()){
return -1;
}
if(tail!=0)return queue[tail-1];
else return queue[queue.length-1];
}

/** Checks whether the circular queue is empty or not. */
public boolean isEmpty() {
if(tail == head && queue[tail] == null){
return true;
}
return false;
}
/** Checks whether the circular queue is full or not. */
public boolean isFull() {
if(head == tail && queue[tail] != null){
return true;
}
return false;
}
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue obj = new MyCircularQueue(k);
* boolean param_1 = obj.enQueue(value);
* boolean param_2 = obj.deQueue();
* int param_3 = obj.Front();
* int param_4 = obj.Rear();
* boolean param_5 = obj.isEmpty();
* boolean param_6 = obj.isFull();
*/

总结

思路2,看别人的分享有的思路
只需要头,尾指针,和数组
head指针存有数据,tail是最后一个有数据的位的下一个,是一个空指针
队列满,用tail==head,tail!=null,
队列空,tail== head,tail ==null
满或空的都是通过,头尾指针重合,如果尾指针是不是空来判断,这样要保证数据可以存空值
deQueue和deQueue都比较好处理,不用判断特殊的条件
rear要取前一个值,这个要特殊注意。
总结
难点1,通过tail指针指向空的下一个位,来让队列的满或空,很好判断,不会出现特殊位置的判断,很巧妙

Contents
  1. 1. 题目
  2. 2. 思考思路
  3. 3. 解题过程
    1. 3.1. 解法1
      1. 3.1.1. 总结
    2. 3.2. 解法2
      1. 3.2.1. 总结