#include <stack>
#include <iostream>
using namespace std;
class Queue{
stack<int> stackPush;
stack<int> stackPop;
public:
void add(int n) {
stackPush.push(n);
}
int pop() {
if (stackPush.empty() && stackPop.empty())
cout << "队列为空" << endl;
else if (stackPop.empty())
while (!stackPush.empty())
{
int i = stackPush.top();
stackPop.push(i);
stackPush.pop();
}
int p;
p= stackPop.top();
stackPop.pop();
while (!stackPop.empty())
{
int i = stackPop.top();
stackPush.push(i);
stackPop.pop();
}
return p;
}
};
void main() {
int i,b;
cout <<"请输入入队元素个数:"<< endl;
cin >> i;
cout << "请输入入队元素:" << endl;
Queue queue;
for (int j = 0; j < i; j++) {
int a;
cin >> a;
queue.add(a);
}
cout << "入队完毕,请输入出队元素个数" << endl;
cin >> b;
cout << "出队元素为:" << endl;
for (int j = 0; j < b; j++) {
int s = queue.pop();
cout <<s<< endl;
}
system("pause");
}
这里写了进出队功能,要想获得队头队尾元素只需要把class里边的两个函数稍微改一下,这里就不写了~~懒~~~