循环队列

用数组构造循环队列。

UPD: 2023-10-21 UPD: 2025-07-13

循环队列

功能

节省空间。

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

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

实际比赛中根本用不到(现在这年头还有谁手写数据结构啊),直接用 STL queue。

代码

 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
#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;
    }
};
使用 Hugo 构建
主题 StackJimmy 设计