hello-algo/codes/cpp/chapter_stack_and_queue/deque.cpp

47 lines
1.1 KiB
C++
Raw Permalink Normal View History

/**
* File: deque.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
2022-11-30 02:27:26 +08:00
/* Driver Code */
int main() {
/* 初始化双向队列 */
deque<int> deque;
/* 元素入队 */
deque.push_back(2);
deque.push_back(5);
deque.push_back(4);
deque.push_front(3);
deque.push_front(1);
cout << "双向队列 deque = ";
printDeque(deque);
2022-11-30 02:27:26 +08:00
/* 访问元素 */
2022-11-30 02:27:26 +08:00
int front = deque.front();
cout << "队首元素 front = " << front << endl;
int back = deque.back();
cout << "队尾元素 back = " << back << endl;
/* 元素出队 */
deque.pop_front();
cout << "队首出队元素 popFront = " << front << ",队首出队后 deque = ";
printDeque(deque);
2022-11-30 02:27:26 +08:00
deque.pop_back();
cout << "队尾出队元素 popLast = " << back << ",队尾出队后 deque = ";
printDeque(deque);
2022-11-30 02:27:26 +08:00
/* 获取双向队列的长度 */
int size = deque.size();
cout << "双向队列长度 size = " << size << endl;
/* 判断双向队列是否为空 */
bool empty = deque.empty();
cout << "双向队列是否为空 = " << empty << endl;
return 0;
}