// ç¨éåå®ç°æ
// æè·¯ ç¨ä¸¤ä¸ªéå
#include
#include
#include
using namespace std;
class MyStack{
private:
queue q1;
queue q2;
public:
MyStack(){
}
void push(int obj){
q1.push(obj);
}
void pop(){
if (q1.empty()){
cerr << "pop-->your stack is empty" << endl;
}
else if (q1.size() == 1){
q1.pop();
}
else{
int size = q1.size();
//!è¿éå°forå¾ªç¯æ¡ä»¶æ¢æfor (int i = 0; i < q1.size() - 1; i++)
//!åå½é¿åº¦ä¸º20æ¶ï¼å¾ªç¯åªä¼è¿è¡9次,åå æ¯å¾ªç¯ä½å
æpop()æä½ï¼è¿ä¼æ¹åq1.size()çå¼
for (int i = 0; i < size - 1; i++){
int tmp = q1.front();
q1.pop();
q2.push(tmp);
}
q1.pop();
// è¿éä¹å¯ä»¥ç´æ¥å°q1åq2çå¼ç¨äº¤æ¢ä¸ä¸
//!æ¤å¤ä¸ä¸æåï¼éè®°å½q2çé¿åº¦
int size2 = q2.size();
for (int i = 0; i < size2; i++){
int tmp = q2.front();
q2.pop();
q1.push(tmp);
}
}
}
int top(){
if (q1.empty()){
cerr << "top-->your stack is empty" << endl;
return NULL;
}
else{
return q1.back();
}
}
int size(){
return q1.size();
}
};
int main(){
MyStack s1;
cout << "the content:" << endl;
for (int i = 0; i < 20; i++){
s1.push(i);
cout << s1.top() << " ";
}
cout << endl;
cout << "pop:" << endl;
for (int i = 0; i < 20; i++){
cout << s1.top() << " ";
s1.pop();
}
system("pause");
return 0;
}