hello-algo/codes/cpp/chapter_stack_and_queue/deque.cpp
Yudong Jin ebf9024136 Add Go codes to docs, including
the chapter of stack and queue, the chapter of tree.
2022-12-03 20:25:24 +08:00

48 lines
1.2 KiB
C++

/*
* File: deque.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
/* 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 = ";
PrintUtil::printDeque(deque);
/* 访问元素 */
int front = deque.front();
cout << "队首元素 front = " << front << endl;
int back = deque.back();
cout << "队尾元素 back = " << back << endl;
/* 元素出队 */
deque.pop_front();
cout << "队首出队元素 popFront = " << front << ",队首出队后 deque = ";
PrintUtil::printDeque(deque);
deque.pop_back();
cout << "队尾出队元素 popLast = " << back << ",队尾出队后 deque = ";
PrintUtil::printDeque(deque);
/* 获取双向队列的长度 */
int size = deque.size();
cout << "双向队列长度 size = " << size << endl;
/* 判断双向队列是否为空 */
bool empty = deque.empty();
cout << "双向队列是否为空 = " << empty << endl;
return 0;
}