#include <iostream>
#include <stack>
#include <queue>
using namespace std;
//栈
void func1()
{
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
cout << s.top() << endl;
cout << s.size() << endl;
while(!s.empty())
{
cout << s.top()<< endl;
s.pop();
}
}
//队列
void func2()
{
queue<int> q;
q.push(1);
q.push(4);
q.push(5);
cout << q.front()<< endl;
cout << q.back()<< endl;
while(!q.empty())
{
cout << q.front() << endl;
q.pop();
}
}
class MyQueue
{
public:
void push(int a)
{
s1.push(a);
}
void pop()
{
if(s2.empty())
{
while(s1.size() > 1)
{
s2.push(s1.top());
s1.pop();
}
//从s1 出
if(!s1.empty())
s1.pop();
}
else
{
s2.pop();
}
}
private:
stack<int> s1;
stack<int> s2;
};
int main()
{
func2();
return 0;
}
#include <stack>
#include <queue>
using namespace std;
//栈
void func1()
{
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
cout << s.top() << endl;
cout << s.size() << endl;
while(!s.empty())
{
cout << s.top()<< endl;
s.pop();
}
}
//队列
void func2()
{
queue<int> q;
q.push(1);
q.push(4);
q.push(5);
cout << q.front()<< endl;
cout << q.back()<< endl;
while(!q.empty())
{
cout << q.front() << endl;
q.pop();
}
}
class MyQueue
{
public:
void push(int a)
{
s1.push(a);
}
void pop()
{
if(s2.empty())
{
while(s1.size() > 1)
{
s2.push(s1.top());
s1.pop();
}
//从s1 出
if(!s1.empty())
s1.pop();
}
else
{
s2.pop();
}
}
private:
stack<int> s1;
stack<int> s2;
};
int main()
{
func2();
return 0;
}