循环队列

用数组构造循环队列。

循环队列

功能

节省空间。

普通队列用过的空间不能用了,循环队列可以重复利用。

1
push(1); pop(); push(2); pop();

代码

 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
#include <iostream>

using namespace std;

// 队列长度,可以自己修改。
const int N = 10;

// 队列
struct Queue {
    int q[N];
    int hh = 1, tt = 0;

    void push(int x) {
        q[( ++ tt) % N] = x;
        if (hh / N && tt / N) {
            hh -= (int)min(hh / N, tt / N) * N;
            tt -= (int)min(hh / N, tt / N) * N;
        }
    }

    void pop() {
        hh ++ ;
    }

    int front() {
        return q[hh % N];
    }

    int back() {
        return q[tt % N];
    }

    bool empty() {
        return hh > tt;
    }

    int size() {
        return tt - hh + 1;
    }
};

// 调试
int main() {
    Queue q;
    q.push(1);
    q.pop();
    q.push(1);
    q.push(2);
    cout << (q.empty() ? 'Y' : 'N') << endl;
    q.push(3);
    q.pop();
    q.pop();
    q.pop();
    q.push(1);
    q.push(2);
    q.push(3);
    cout << q.front() << ' ';
    cout << q.back() << endl;
    q.push(4);
    q.push(5);
    cout << q.size();
    q.push(6);
    q.pop();
    q.pop();
    q.pop();
    q.pop();
    q.pop();
    q.pop();
    return 0;
}

UPD:2023-10-21

Built with Hugo
主题 StackJimmy 设计