两个栈实现一个队列

  • Post author:
  • Post category:其他


#include <bits/stdc++.h>

using namespace std;

class Solution

{


private:

stack<int>stack1;

stack<int>stack2;

public:

void push(int node)

{


stack1.push(node);

}

int pop()

{


if(stack2.size()!=0)

{


int tmp = stack2.top();

stack2.pop();

return tmp;

}

else

{


while (stack1.size()!=0)

{


int tmp = stack1.top();

stack1.pop();

stack2.push(tmp);

}

return pop();

}

}

};

int main()

{

system(“pause”);

return 0;

}



版权声明:本文为liyuloves原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。