文章目錄
隊列和棧
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
int main()
{
// stack 先進後出,允許棧頂新增,棧頂移除,取得棧頂元素,不允許有遍歷行爲,也就是沒有其他操作。
stack<int> s;
//壓入數據
s.push(10);
s.push(20);
s.push(30);
s.push(40);
while (s.size() != 0) {
cout << s.top() << endl; //查看棧頂元素數值
s.pop(); //彈出數據
}
//不提供遍歷沒有迭代器,只有隊頭元素才能被外界取用,隊尾進入元素
cout<< " queue: " << endl;
queue<int> q;
q.push(10);
q.push(20);
q.push(30);
q.push(40);
while (!q.empty()) //也可以用empty來判斷
{
cout << q.front() << endl; //查看隊頭元素
cout << q.back() << endl; //查看隊尾元素
q.pop();//彈出隊頭數據
}
}