对于队列和栈,它们的操作是受限的:队列只能在一端插入元素,另一端删除元素;栈只能在某一端插入和删除元素。
队列是一种「先进先出」的数据结构,栈是一种「先进后出」的数据结构
栈
用链表实现栈
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
79
80
81
82
83
84
85
86
87
88
89
90
91
| #include <iostream>
#include <list> // 标准库链表
#include <stdexcept> // 异常处理
template <typename T>
class ListStack {
private:
std::list<T> stack; // 底层容器:std::list(双向链表)
public:
// 入栈:在链表头部添加元素(栈顶)
void push(const T& val) {
stack.push_front(val); // O(1) 时间复杂度
}
// 出栈:移除并返回栈顶元素
T pop() {
if (isEmpty()) {
throw std::runtime_error("栈为空,无法出栈");
}
T topVal = stack.front(); // 获取栈顶元素
stack.pop_front(); // 移除栈顶(O(1))
return topVal;
}
// 获取栈顶元素(不删除)
T top() const {
if (isEmpty()) {
throw std::runtime_error("栈为空,无法获取栈顶元素");
}
return stack.front();
}
// 判断栈是否为空
bool isEmpty() const {
return stack.empty();
}
// 获取栈的大小
size_t size() const {
return stack.size();
}
// 打印栈元素(从栈顶到栈底)
void print() const {
if (isEmpty()) {
std::cout << "栈为空" << std::endl;
return;
}
std::cout << "栈顶 -> ";
for (const auto& elem : stack) { // 遍历链表(栈顶到栈底)
std::cout << elem << " -> ";
}
std::cout << "栈底" << std::endl;
}
};
// 测试示例
int main() {
try {
ListStack<int> stack;
// 入栈
stack.push(10);
stack.push(20);
stack.push(30);
std::cout << "入栈后(10, 20, 30):" << std::endl;
stack.print(); // 输出:栈顶 -> 30 -> 20 -> 10 -> 栈底
std::cout << "栈大小:" << stack.size() << std::endl; // 3
// 获取栈顶
std::cout << "栈顶元素:" << stack.top() << std::endl; // 30
// 出栈
std::cout << "出栈元素:" << stack.pop() << std::endl; // 30
stack.print(); // 栈顶 -> 20 -> 10 -> 栈底
std::cout << "栈大小:" << stack.size() << std::endl; // 2
// 清空栈
stack.pop(); // 出栈 20
stack.pop(); // 出栈 10
std::cout << "清空后是否为空:" << (stack.isEmpty() ? "是" : "否") << std::endl; // 是
// 测试空栈操作(抛异常)
stack.pop(); // 触发异常
} catch (const std::exception& e) {
std::cout << "异常:" << e.what() << std::endl; // 输出错误信息
}
return 0;
}
|
用数组实现栈
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
| #include <iostream>
#include <vector> // 标准库动态数组
#include <stdexcept> // 异常处理
template <typename T>
class VectorStack {
private:
std::vector<T> stack; // 底层容器:std::vector(动态数组)
public:
// 入栈:在数组尾部添加元素(栈顶)
void push(const T& val) {
stack.push_back(val); // O(1) 操作( amortized,平均复杂度)
}
// 出栈:移除并返回栈顶元素(数组尾部)
T pop() {
if (isEmpty()) {
throw std::runtime_error("栈为空,无法出栈");
}
T topVal = stack.back(); // 获取尾部元素(栈顶)
stack.pop_back(); // 移除尾部元素(O(1))
return topVal;
}
// 获取栈顶元素(不删除)
T top() const {
if (isEmpty()) {
throw std::runtime_error("栈为空,无法获取栈顶元素");
}
return stack.back();
}
// 判断栈是否为空
bool isEmpty() const {
return stack.empty();
}
// 获取栈的大小
size_t size() const {
return stack.size();
}
// 打印栈元素(从栈顶到栈底)
void print() const {
if (isEmpty()) {
std::cout << "栈为空" << std::endl;
return;
}
std::cout << "栈顶 -> ";
// 从尾部(栈顶)遍历到头部(栈底)
for (auto it = stack.rbegin(); it != stack.rend(); ++it) {
std::cout << *it << " -> ";
}
std::cout << "栈底" << std::endl;
}
};
// 测试示例
int main() {
try {
VectorStack<int> stack;
// 入栈操作
stack.push(10);
stack.push(20);
stack.push(30);
std::cout << "入栈后(10, 20, 30):" << std::endl;
stack.print(); // 输出:栈顶 -> 30 -> 20 -> 10 -> 栈底
std::cout << "栈大小:" << stack.size() << std::endl; // 3
// 获取栈顶元素
std::cout << "栈顶元素:" << stack.top() << std::endl; // 30
// 出栈操作
std::cout << "出栈元素:" << stack.pop() << std::endl; // 30
stack.print(); // 输出:栈顶 -> 20 -> 10 -> 栈底
std::cout << "栈大小:" << stack.size() << std::endl; // 2
// 清空栈
stack.pop(); // 出栈 20
stack.pop(); // 出栈 10
std::cout << "清空后是否为空:" << (stack.isEmpty() ? "是" : "否") << std::endl; // 是
// 测试空栈操作(抛异常)
stack.pop(); // 触发异常
} catch (const std::exception& e) {
std::cout << "异常:" << e.what() << std::endl; // 输出错误信息
}
return 0;
}
|
队列
用链表实现队列
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
| #include <iostream>
#include <list> // 标准库双向链表
#include <stdexcept> // 异常处理
template <typename T>
class ListQueue {
private:
std::list<T> queue; // 底层容器:std::list
public:
// 入队:在链表尾部添加元素(队尾)
void enqueue(const T& val) {
queue.push_back(val); // O(1) 操作
}
// 出队:移除并返回链表头部元素(队首)
T dequeue() {
if (isEmpty()) {
throw std::runtime_error("队列为空,无法出队");
}
T frontVal = queue.front(); // 获取队首元素
queue.pop_front(); // 移除队首(O(1) 操作)
return frontVal;
}
// 获取队首元素(不删除)
T front() const {
if (isEmpty()) {
throw std::runtime_error("队列为空,无法获取队首元素");
}
return queue.front();
}
// 获取队尾元素(不删除)
T back() const {
if (isEmpty()) {
throw std::runtime_error("队列为空,无法获取队尾元素");
}
return queue.back();
}
// 判断队列是否为空
bool isEmpty() const {
return queue.empty();
}
// 获取队列大小
size_t size() const {
return queue.size();
}
// 打印队列元素(从队首到队尾)
void print() const {
if (isEmpty()) {
std::cout << "队列为空" << std::endl;
return;
}
std::cout << "队首 -> ";
for (const auto& elem : queue) { // 遍历链表(队首到队尾)
std::cout << elem << " -> ";
}
std::cout << "队尾" << std::endl;
}
};
// 测试示例
int main() {
try {
ListQueue<int> q;
// 入队操作
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
std::cout << "入队后(10, 20, 30):" << std::endl;
q.print(); // 输出:队首 -> 10 -> 20 -> 30 -> 队尾
std::cout << "队列大小:" << q.size() << std::endl; // 3
// 获取队首和队尾
std::cout << "队首元素:" << q.front() << std::endl; // 10
std::cout << "队尾元素:" << q.back() << std::endl; // 30
// 出队操作
std::cout << "出队元素:" << q.dequeue() << std::endl; // 10
q.print(); // 输出:队首 -> 20 -> 30 -> 队尾
std::cout << "队列大小:" << q.size() << std::endl; // 2
// 清空队列
q.dequeue(); // 出队 20
q.dequeue(); // 出队 30
std::cout << "清空后是否为空:" << (q.isEmpty() ? "是" : "否") << std::endl; // 是
// 测试空队列操作(抛异常)
q.dequeue(); // 触发异常
} catch (const std::exception& e) {
std::cout << "异常:" << e.what() << std::endl; // 输出错误信息
}
return 0;
}
|
用数组实现队列
vector 数组,实现的话,需要搬迁数据,删除/出队的时间复杂度为 O(n),环形数组则为O(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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
| #include <iostream>
#include <vector>
#include <stdexcept>
// 环形数组类(复用提供的实现)
template <typename T>
class CycleArray {
private:
std::vector<T> buffer_;
size_t capacity_;
size_t head_; // 下一个要读的元素的位置(队首)
size_t tail_; // 下一个要写的元素的位置(队尾)
size_t size_; // 当前元素数量
public:
// 构造函数,初始化容量
explicit CycleArray(size_t capacity)
: capacity_(capacity), head_(0), tail_(0), size_(0)
{
if (capacity == 0) {
throw std::invalid_argument("Capacity must be greater than zero.");
}
buffer_.resize(capacity);
}
// 检查环形数组是否为空
bool empty() const {
return size_ == 0;
}
// 检查环形数组是否已满
bool full() const {
return size_ == capacity_;
}
// 获取当前存储的元素数量
size_t size() const {
return size_;
}
// 获取容量
size_t capacity() const {
return capacity_;
}
// 插入元素 (EnQueue 方向)
void push(const T& item) {
if (full()) {
// 满时覆盖最旧元素(队首后移)
head_ = (head_ + 1) % capacity_;
} else {
size_++;
}
buffer_[tail_] = item;
tail_ = (tail_ + 1) % capacity_;
}
// 弹出元素 (DeQueue 方向)
T pop() {
if (empty()) {
throw std::out_of_range("CycleArray is empty. Cannot pop.");
}
T item = buffer_[head_];
head_ = (head_ + 1) % capacity_;
size_--;
return item;
}
// 查看头部元素
const T& front() const {
if (empty()) {
throw std::out_of_range("CycleArray is empty. No front element.");
}
return buffer_[head_];
}
};
// 基于环形数组的队列类
template <typename T>
class CircularQueue {
private:
CycleArray<T> cycle_arr_; // 底层环形数组
public:
// 构造函数:指定队列容量
explicit CircularQueue(size_t capacity) : cycle_arr_(capacity) {}
// 入队:在队尾添加元素(满时覆盖最旧元素)
void enqueue(const T& item) {
cycle_arr_.push(item); // 复用环形数组的 push 操作
}
// 出队:移除并返回队首元素
T dequeue() {
return cycle_arr_.pop(); // 复用环形数组的 pop 操作
}
// 查看队首元素(不删除)
const T& front() const {
return cycle_arr_.front(); // 复用环形数组的 front 操作
}
// 判断队列是否为空
bool isEmpty() const {
return cycle_arr_.empty();
}
// 判断队列是否已满
bool isFull() const {
return cycle_arr_.full();
}
// 获取当前元素数量
size_t size() const {
return cycle_arr_.size();
}
// 获取队列容量
size_t capacity() const {
return cycle_arr_.capacity();
}
// 打印队列元素(从队首到队尾)
void print() const {
if (isEmpty()) {
std::cout << "Queue is empty." << std::endl;
return;
}
std::cout << "Queue (size: " << size() << ", capacity: " << capacity() << "):";
std::cout << "front -> ";
// 临时拷贝环形数组数据,按顺序打印(避免修改原数组)
CycleArray<T> temp(cycle_arr_.capacity());
size_t current_size = size();
for (size_t i = 0; i < current_size; ++i) {
T item = cycle_arr_.front();
temp.push(item);
std::cout << item << " -> ";
// 临时移动原数组 head_(打印后恢复)
const_cast<CycleArray<T>&>(cycle_arr_).pop();
}
// 恢复原数组数据
for (size_t i = 0; i < current_size; ++i) {
T item = temp.pop();
const_cast<CycleArray<T>&>(cycle_arr_).push(item);
}
std::cout << "rear" << std::endl;
}
};
// 测试示例
int main() {
try {
// 创建容量为 3 的队列
CircularQueue<int> queue(3);
// 入队操作
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.print(); // 输出:front -> 10 -> 20 -> 30 -> rear(size:3, capacity:3)
// 队列已满,继续入队会覆盖队首(10)
queue.enqueue(40);
queue.print(); // 输出:front -> 20 -> 30 -> 40 -> rear(size:3, capacity:3)
// 出队操作
std::cout << "Dequeue: " << queue.dequeue() << std::endl; // 出队 20
queue.print(); // 输出:front -> 30 -> 40 -> rear(size:2, capacity:3)
// 查看队首
std::cout << "Front element: " << queue.front() << std::endl; // 30
// 继续入队
queue.enqueue(50);
queue.print(); // 输出:front -> 30 -> 40 -> 50 -> rear(size:3, capacity:3)
// 清空队列
while (!queue.isEmpty()) {
std::cout << "Dequeue: " << queue.dequeue() << std::endl; // 30,40,50
}
queue.print(); // 输出:Queue is empty.
} catch (const std::exception& e) {
std::cout << "Error: " << e.what() << std::endl;
}
return 0;
}
|
双端队列
比标准队列增加了些操作,可以在两端进行插入和删除
用链表实现双端队列
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
| #include <iostream>
#include <list> // 标准库双向链表
#include <stdexcept> // 异常处理
template <typename T>
class LinkedListDeque {
private:
std::list<T> deque; // 底层容器:std::list(双向链表)
public:
// 队首入队(头部插入)
void push_front(const T& val) {
deque.push_front(val); // O(1) 操作
}
// 队尾入队(尾部插入)
void push_back(const T& val) {
deque.push_back(val); // O(1) 操作
}
// 队首出队(移除并返回头部元素)
T pop_front() {
if (isEmpty()) {
throw std::runtime_error("双端队列为空,无法从队首出队");
}
T frontVal = deque.front(); // 获取头部元素
deque.pop_front(); // 移除头部元素(O(1))
return frontVal;
}
// 队尾出队(移除并返回尾部元素)
T pop_back() {
if (isEmpty()) {
throw std::runtime_error("双端队列为空,无法从队尾出队");
}
T backVal = deque.back(); // 获取尾部元素
deque.pop_back(); // 移除尾部元素(O(1))
return backVal;
}
// 查看队首元素(不删除)
const T& front() const {
if (isEmpty()) {
throw std::runtime_error("双端队列为空,无队首元素");
}
return deque.front();
}
// 查看队尾元素(不删除)
const T& back() const {
if (isEmpty()) {
throw std::runtime_error("双端队列为空,无队尾元素");
}
return deque.back();
}
// 判断队列是否为空
bool isEmpty() const {
return deque.empty();
}
// 获取队列大小
size_t size() const {
return deque.size();
}
// 打印双端队列元素(从队首到队尾)
void print() const {
if (isEmpty()) {
std::cout << "双端队列为空" << std::endl;
return;
}
std::cout << "队首 -> ";
for (const auto& elem : deque) { // 遍历链表(队首到队尾)
std::cout << elem << " -> ";
}
std::cout << "队尾" << std::endl;
}
};
// 测试示例
int main() {
try {
LinkedListDeque<int> deque;
// 队尾入队
deque.push_back(10);
deque.push_back(20);
std::cout << "队尾入队 10, 20 后:" << std::endl;
deque.print(); // 输出:队首 -> 10 -> 20 -> 队尾
std::cout << "大小:" << deque.size() << std::endl; // 2
// 队首入队
deque.push_front(5);
std::cout << "队首入队 5 后:" << std::endl;
deque.print(); // 输出:队首 -> 5 -> 10 -> 20 -> 队尾
std::cout << "大小:" << deque.size() << std::endl; // 3
// 查看首尾元素
std::cout << "队首元素:" << deque.front() << std::endl; // 5
std::cout << "队尾元素:" << deque.back() << std::endl; // 20
// 队首出队
std::cout << "队首出队:" << deque.pop_front() << std::endl; // 5
deque.print(); // 输出:队首 -> 10 -> 20 -> 队尾
std::cout << "大小:" << deque.size() << std::endl; // 2
// 队尾出队
std::cout << "队尾出队:" << deque.pop_back() << std::endl; // 20
deque.print(); // 输出:队首 -> 10 -> 队尾
std::cout << "大小:" << deque.size() << std::endl; // 1
// 清空队列
deque.pop_front();
std::cout << "清空后是否为空:" << (deque.isEmpty() ? "是" : "否") << std::endl; // 是
// 测试空队列操作(抛异常)
deque.pop_front(); // 触发异常
} catch (const std::exception& e) {
std::cout << "异常:" << e.what() << std::endl; // 输出错误信息
}
return 0;
}
|
用数组实现双端队列
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
| #include <iostream>
#include <vector>
#include <stdexcept>
// 环形数组类(复用基础实现,扩展双端操作所需的指针移动逻辑)
template <typename T>
class CycleArray {
protected: // 保护成员,允许派生类访问
std::vector<T> buffer_;
size_t capacity_;
size_t head_; // 队首(下一个要读的位置)
size_t tail_; // 队尾(下一个要写的位置)
size_t size_; // 当前元素数量
public:
// 构造函数:初始化容量
explicit CycleArray(size_t capacity)
: capacity_(capacity), head_(0), tail_(0), size_(0)
{
if (capacity == 0) {
throw std::invalid_argument("Capacity must be greater than zero.");
}
buffer_.resize(capacity);
}
// 基础状态判断
bool empty() const { return size_ == 0; }
bool full() const { return size_ == capacity_; }
size_t size() const { return size_; }
size_t capacity() const { return capacity_; }
protected:
// 辅助函数:计算上一个索引(用于队首入队)
size_t prev_index(size_t index) const {
return (index == 0) ? capacity_ - 1 : index - 1;
}
// 辅助函数:计算下一个索引(用于队尾入队/常规出队)
size_t next_index(size_t index) const {
return (index + 1) % capacity_;
}
};
// 基于环形数组的双端队列
template <typename T>
class CircularDeque : public CycleArray<T> {
public:
// 继承构造函数
using CycleArray<T>::CycleArray;
// 队首入队(头部插入)
void push_front(const T& item) {
if (this->full()) {
throw std::overflow_error("双端队列已满,无法从队首入队");
// 若需覆盖逻辑,可改为:pop_back(); 再执行以下操作
}
// 队首指针前移(环形处理)
this->head_ = this->prev_index(this->head_);
// 存入新元素
this->buffer_[this->head_] = item;
this->size_++;
}
// 队尾入队(尾部插入)
void push_back(const T& item) {
if (this->full()) {
throw std::overflow_error("双端队列已满,无法从队尾入队");
// 若需覆盖逻辑,可改为:pop_front(); 再执行以下操作
}
// 存入新元素(tail_ 原本指向空闲位置)
this->buffer_[this->tail_] = item;
// 队尾指针后移(环形处理)
this->tail_ = this->next_index(this->tail_);
this->size_++;
}
// 队首出队(移除并返回头部元素)
T pop_front() {
if (this->empty()) {
throw std::underflow_error("双端队列为空,无法从队首出队");
}
T item = this->buffer_[this->head_];
// 队首指针后移
this->head_ = this->next_index(this->head_);
this->size_--;
return item;
}
// 队尾出队(移除并返回尾部元素)
T pop_back() {
if (this->empty()) {
throw std::underflow_error("双端队列为空,无法从队尾出队");
}
// 队尾指针前移(指向最后一个元素)
this->tail_ = this->prev_index(this->tail_);
T item = this->buffer_[this->tail_];
this->size_--;
return item;
}
// 查看队首元素
const T& front() const {
if (this->empty()) {
throw std::underflow_error("双端队列为空,无队首元素");
}
return this->buffer_[this->head_];
}
// 查看队尾元素
const T& back() const {
if (this->empty()) {
throw std::underflow_error("双端队列为空,无队尾元素");
}
// tail_ 指向空闲位置,实际队尾是前一个索引
size_t last_index = this->prev_index(this->tail_);
return this->buffer_[last_index];
}
// 打印双端队列(从队首到队尾)
void print() const {
if (this->empty()) {
std::cout << "双端队列为空" << std::endl;
return;
}
std::cout << "双端队列(大小: " << this->size()
<< ", 容量: " << this->capacity() << "):";
std::cout << "队首 -> ";
size_t current = this->head_;
for (size_t i = 0; i < this->size(); ++i) {
std::cout << this->buffer_[current] << " -> ";
current = this->next_index(current);
}
std::cout << "队尾" << std::endl;
}
};
// 测试示例
int main() {
try {
CircularDeque<int> deque(4); // 容量为 4 的双端队列
// 队尾入队
deque.push_back(10);
deque.push_back(20);
deque.print(); // 队首 -> 10 -> 20 -> 队尾(size:2)
// 队首入队
deque.push_front(5);
deque.push_front(3);
deque.print(); // 队首 -> 3 -> 5 -> 10 -> 20 -> 队尾(size:4,已满)
// 测试队列满时入队(抛异常)
// deque.push_back(30); // 触发 "已满" 异常
// 查看首尾元素
std::cout << "队首元素:" << deque.front() << std::endl; // 3
std::cout << "队尾元素:" << deque.back() << std::endl; // 20
// 队首出队
std::cout << "队首出队:" << deque.pop_front() << std::endl; // 3
deque.print(); // 队首 -> 5 -> 10 -> 20 -> 队尾(size:3)
// 队尾出队
std::cout << "队尾出队:" << deque.pop_back() << std::endl; // 20
deque.print(); // 队首 -> 5 -> 10 -> 队尾(size:2)
// 继续队首入队
deque.push_front(2);
deque.print(); // 队首 -> 2 -> 5 -> 10 -> 队尾(size:3)
// 清空队列
while (!deque.empty()) {
std::cout << "队尾出队:" << deque.pop_back() << std::endl; // 10,5,2
}
deque.print(); // 双端队列为空
} catch (const std::exception& e) {
std::cout << "异常:" << e.what() << std::endl;
}
return 0;
}
|
Reference
https://labuladong.online/algo/data-structure-basic/linkedlist-basic/