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

    用两个栈实现队列。队列声明如下,请实现它的两个函数append_tail和delete_head在队列的队尾添加数据和删除队头 

    class Queue_by_stack
    {
        public:
            Queue_by_stack(){};
            ~Queue_by_stack(){};
            void append_tail(const T& node);
            T delete_head();
            void Show_Queue(void); //从队首到队尾依次输出队列数据
        protected:
        private:
            stack<T> stack1;
            stack<T> stack2;
    };

    分析:队列的特性是先进后出,比如对于1,2,3,4序列来说,首先将序列压入栈(stack1和stack2随便选一个),要实现delete_head的话,不能对stack1进行pop,因为这时stack1的top值是4,我们要删除的是1,注意这时候stack2还没有利用,如果我将stack1里的数据都逐个pop,push进去stack2,对于stack2;来说,那不就正好相当于原序列吗?实现delete_head的话只需对stack2进行pop就行了,append_tail的话只需往空的stack1里面push就行了。代码实现如下 

    #include <stack>
    #include <iostream>
    using namespace std;
    template<typename T>
    class Queue_by_stack
    {
        public:
            Queue_by_stack(){};
            ~Queue_by_stack(){};
            void append_tail(const T& node);
            T delete_head();
            void Show_Queue(void); //从队首到队尾依次输出队列数据
        protected:
        private:
            stack<T> stack1;
            stack<T> stack2;
    };
    template<typename T>
    void Queue_by_stack<T>::Show_Queue(void)
    {
        T tmp;
        stack<T>  S1(stack1); // 将stack1的数据copy到S1
        stack<T>  S2(stack2);
        cout<<"
    This is Show_Queue !
    ";
        while(!S2.empty())  //先把S2中的所有数据清仓输出
        {
            cout<<S2.top()<<"  ";
            S2.pop();
        }
        while(!S1.empty()) //再把S1中的数据全部倒进S2
        {
                tmp = S1.top();
                S2.push(tmp);
                S1.pop();
        }
        while(!S2.empty())//再把S2中的所有数据清仓输出
        {
            cout<<S2.top()<<"  ";
            S2.pop();
        }
    
    }
    
    template<typename T>
    T Queue_by_stack<T>::delete_head()
    {
        T tmp;
        if (stack2.empty())  //删除元素  先判断栈2是否为空
        {
            while (!stack1.empty())  //栈2为空栈,把栈1中的数据全部倒入栈2
            {
                tmp = stack1.top();
                stack2.push(tmp);
                stack1.pop();
            }
        }
        if (stack2.empty()) //栈2仍为空,说明队列为空
        {
            return -1;
        }
        tmp = stack2.top();//如果栈2 不空的情况下,则栈2的栈顶就是队列的队首
        stack2.pop();
        return tmp;
    }
    template<typename T>
    void Queue_by_stack<T>::append_tail( const T& node )
    {
        stack1.push(node);
    }
    int main()
    {
        Queue_by_stack<char> my_queue;
        my_queue.append_tail('a');
        my_queue.append_tail('b');
        my_queue.append_tail('c');
        my_queue.Show_Queue();
    
    
        my_queue.delete_head();
        my_queue.delete_head();
        my_queue.Show_Queue();
    
        my_queue.append_tail('d');
        my_queue.append_tail('e');
        my_queue.Show_Queue();
    
        my_queue.delete_head();
        my_queue.Show_Queue();
    }
    /**********************************
    入栈顺序是 a b c d e
    运行结果:
    
    This is Show_Queue !
    a  b  c
    This is Show_Queue !
    c
    This is Show_Queue !
    c  d  e
    This is Show_Queue !
    d  e
    ***********************************/
    


  • 相关阅读:
    java异常
    Map集和
    获取每个字符出现的次数
    从1-33号球中选取6个红球,且红球数字最多重复不超过3个 从1-16号球中选取一个篮球 由红球和蓝球共同组成一个双色球号码,且红球在左边(按照升序排列),篮球在右边。
    gitlab介绍及使用
    Maven使用介绍
    IDEA集成开发环境安装git,修改代码后文件变色代表的含义
    大数据相关
    开源镜像站汇总
    MySQL配置文件详解
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3233839.html
Copyright © 2011-2022 走看看