用两个栈实现队列

来源:互联网 发布:在mac上用win to go 编辑:程序博客网 时间:2024/06/07 20:24

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。



using System.Collections.Generic;
class Solution
{
Stack stackPop=new Stack();
Stack stackPush=new Stack();
public void push(int node)
{
stackPush.Push(node);
}

public int pop() {    while(stackPush.Count!=0)    stackPop.Push(stackPush.Pop());            int res=stackPop.Pop();    while(stackPop.Count!=0)    stackPush.Push(stackPop.Pop());    return res;}

}

0 0