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

48 lines
1.2 KiB
C++
Raw Permalink Normal View History

/*
* File: deque.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
2022-11-29 18:27:26 +00: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 = ";
PrintUtil::printDeque(deque);
/* 访问元素 */
2022-11-29 18:27:26 +00:00
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;
}