push)、隊頭刪除(pop)、隊頭/隊尾存取(front/back)deque 實現#include
#include
using namespace std;
int main() {
// (1) 建立 queue
queue q1; // 空隊列
queue q2(q1); // 複製建構
// (2) push():隊尾插入
q1.push(2);
q1.push(4);
q1.push(6);
// (3) front()/back():存取隊頭/隊尾
cout << "隊頭元素:" << q1.front() << endl; // 2
cout << "隊尾元素:" << q1.back() << endl; // 6
// (4) pop():刪除隊頭
q1.pop(); // 移除 2
// (5) empty():判空
cout << "是否為空:" << q1.empty() << endl; // 0 (false)
// (6) size():元素個數
cout << "隊列長度:" << q1.size() << endl; // 2
return 0;
}
Queue 廣泛應用於廣度優先搜尋(BFS)、任務調度、緩衝區管理、生產者-消費者模型等演算法與系統設計中。掌握其基本操作是解決此類問題的基礎。