zoukankan      html  css  js  c++  java
  • 题目9:用两个栈实现队列

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

    难度⭐

    思路

    stack1 正向队列 用于添加。

    stack2 反向队列 用于删除。

    我的代码。

    class Solution
    {
    public:
        void push(int node) {
            stack1.push(node);
        }
    
        int pop() { 
            if(stack2.size()<=0)
            {
                while(stack1.size()>0)
                {
                    stack2.push(stack1.top());
                    stack1.pop();
                }
            }
            if(stack2.size()==0)
            {
                throw exception();
            }
            int a=stack2.top();
            stack2.pop();
            return a;
        }
    
    private:
        stack<int> stack1;
        stack<int> stack2;
    };

    vs2005上编译。

    注意 T& 的含义,代表引用。

    模板的用法。

    #pragma once
    #include <stack>
    #include <exception>
    
    using namespace std;
    
    template <typename T> class CQueue
    {
    public:
        CQueue(void);
        ~CQueue(void);
        
        // 在队列末尾添加一个结点
        void appendTail(const T& node);
    
        // 删除队列的头结点
        T deleteHead();
    
    private:
        stack<T> stack1;
        stack<T> stack2;
    };
    
    template <typename T> CQueue<T>::CQueue(void)
    {
    }
    
    template <typename T> CQueue<T>::~CQueue(void)
    {
    }
    
     //我的代码
    template<typename T> void CQueue<T>::appendTail(const T& element)
    {
    	 stack1.push(element);
    } 
    
    template<typename T> T CQueue<T>::deleteHead()
    {
    	if(stack2.size()<=0)
    	{
    		//先从头开始压入栈中
    		while(stack1.size()>0)
    		{
    			stack2.push(stack1.top());
    			stack1.pop();
    		}
    	}
    	if (stack2.size()==0)
    	{
    		throw exception(); 
    	}
    	
    	T& head=stack2.top();
    	stack2.pop();
     
    
    	return head ;
    }

    python

    增#直接push 到 stack1

    删#如果stack2 是空的就从stack1 取数(while),然后pop stack2.否则异常

    class Solution:
        def __init__(self):
            self.stackA = []
            self.stackB = []
        def push(self, node):
            self.stackA.append(node)
        def pop(self):
            if self.stackB:
                return self.stackB.pop()
            elif self.stackA == []:
                return None
            else:
                while self.stackA:
                    self.stackB.append(self.stackA.pop())
                return self.stackB.pop()

    java

    删#对stack2判空,如果空就取,不空就.pop(.top())

    import java.util.Stack;
    
    public class Solution {
        Stack<Integer> stack1 = new Stack<Integer>();
        Stack<Integer> stack2 = new Stack<Integer>();
        
        public void push(int node) {
            stack1.push(node);
        }
        
        public int pop() {
            if(stack1.empty()&&stack2.empty())
            {
                throw new RuntimeException("Queue is empty!");
            }
            if(stack2.empty())
            {
                while(!stack1.empty())
                {
                    stack2.push(stack1.pop());
                }
            }
            return stack2.pop();
        }
    }

    相关题目

    两个队列描述一个栈

  • 相关阅读:
    关于lockkeyword
    关于多层for循环迭代的效率优化问题
    Android 面试精华题目总结
    Linux基础回想(1)——Linux系统概述
    linux源代码编译安装OpenCV
    校赛热身 Problem C. Sometimes Naive (状压dp)
    校赛热身 Problem C. Sometimes Naive (状压dp)
    校赛热身 Problem B. Matrix Fast Power
    校赛热身 Problem B. Matrix Fast Power
    集合的划分(递推)
  • 原文地址:https://www.cnblogs.com/lightmare/p/10398845.html
Copyright © 2011-2022 走看看