1-栈.cpp
#include <iostream>
#include <stack>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
stack<int> s;
srand(time(NULL));
int num;
for (int i = 0; i < 10; i++)
{
num = rand() % 10;
s.push(num);
cout << num << "进栈成功" << endl;
}
cout << "栈顶元素是 " << s.top() << endl;
cout << "栈的大小" << s.size() << endl;
//栈和队列不存在遍历,不存在迭代器
//s.begin();
while (!s.empty())
{
cout << s.top() << "出栈" << endl;
s.pop();
}
return 0;
}
2-队列.cpp
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue<int> q;
for (int i = 0; i < 10; i++)
{
q.push(i);
cout << i << "进队成功" << endl;
}
cout << "队头元素" << q.front() << endl;
cout << "队尾元素" << q.back() << endl;
cout << "队列大小" << q.size() << endl;
while (!q.empty())
{
cout << q.front() << "出队" << endl;
q.pop();
}
return 0;
}