Files
hello-algo/en/codes/cpp/chapter_stack_and_queue/deque.cpp
T
Yudong JinandClaude Opus 5.5 eeb8a3b790 Cite demo-source regions from the remaining A-class snippets (batch 2) (#2035)
Migrate init_array, hash_map_operations, hash_map_traversal,
queue_operations, deque_operations and insert_remove_node: zh and ru
demo sources gain region markers and the snippets' comments, en, ja and
ko are regenerated from reviewed translations, zh-hant via OpenCC. 70
blocks become region references; builtin_hash and five blocks without
reviewed translations stay literal.

The Java, Kotlin and Rust deque demos follow the book's order, and a few
demo comments and typos are aligned with the snippets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-16 04:23:45 +08:00

63 lines
1.7 KiB
C++

/**
* File: deque.cpp
* Created Time: 2022-11-25
* Author: krahets ([email protected])
*/
#include "../utils/common.hpp"
/* Driver code */
int main() {
// [region]{init_deque}
/* Initialize the deque */
deque<int> deque;
// [endregion]{init_deque}
// [region]{push}
/* Enqueue elements */
deque.push_back(2); // Add to the rear
deque.push_back(5);
deque.push_back(4);
deque.push_front(3); // Add to the front
deque.push_front(1);
// [endregion]{push}
cout << "Deque deque = ";
printDeque(deque);
// [region]{peek_first}
/* Access an element */
int front = deque.front(); // Front element
// [endregion]{peek_first}
cout << "Front element front = " << front << endl;
// [region]{peek_last}
int back = deque.back(); // Rear element
// [endregion]{peek_last}
cout << "Rear element back = " << back << endl;
// [region]{pop_first}
/* Dequeue an element */
deque.pop_front(); // Dequeue the front element
// [endregion]{pop_first}
cout << "Element removed from the front, popFront = " << front << ", after dequeuing from the front, deque = ";
printDeque(deque);
// [region]{pop_last}
deque.pop_back(); // Dequeue the rear element
// [endregion]{pop_last}
cout << "Element removed from the rear, popLast = " << back << ", after dequeuing from the rear, deque = ";
printDeque(deque);
// [region]{size}
/* Get the deque size */
int size = deque.size();
// [endregion]{size}
cout << "Deque size = " << size << endl;
// [region]{is_empty}
/* Check whether the deque is empty */
bool empty = deque.empty();
// [endregion]{is_empty}
cout << "Is the deque empty = " << empty << endl;
return 0;
}