(c#)用两个栈来实现一个队列

class Solution
    {
        Stack<int> pushStack = new Stack<int>();
        Stack<int> popStack = new Stack<int>();
        
        public void push(int node)
        {
            while (popStack.Count > 0)
            {
                pushStack.Push(popStack.Pop());
            }
            pushStack.Push(node);
        }

        public int pop()
        {
            while (pushStack.Count > 0)
            {
                popStack.Push(pushStack.Pop());
            }
            return popStack.Pop();
        }
    }
pushStack实现入队,借助popStack过渡实现出队,当popStack不为空时,直接弹出,为空时,先将pushStackt弹出压入popStack
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页