zoukankan      html  css  js  c++  java
  • 《剑指offer》第九题:用两个栈实现队列

    // 面试题9:用两个栈实现队列
    // 题目:用两个栈实现一个队列。队列的声明如下,请实现它的两个函数appendTail
    // 和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能。
    
    #pragma once
    #include <stack>
    #include <exception>
    
    using namespace std;
    
    template <typename T> class CQueue //模板, T为类型名
    {
    public:
        CQueue(void); //构造函数
        ~CQueue(void); //析构函数
    
        // 在队列末尾添加一个结点
        void appendTail(const T& node);
    
        // 删除队列的头结点
        T deleteHead();
    
    private:
        stack<T> stack1;
        stack<T> stack2;
    };
    
    //CQueue<T>:: 为作用域
    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
        stack1.push(element);
    }
    
    template<typename T> T CQueue<T>::deleteHead()
    {
        //如果stack2为空,则从stack1中载入数据
        if (stack2.size() <= 0)
        {
            while (stack1.size() > 0)
            {
                T& data = stack1.top();
                stack1.pop();
                stack2.push(data);
            }
        }
    
        //如果载入完还没数据, 则序列为空
        if (stack2.size() == 0)
            throw new exception("Empty queue");
    
        //弹出头部数据
        T head = stack2.top();
        stack2.pop();
    
        return head;
    
    }
    // 面试题9:用两个栈实现队列
    // 题目:用两个栈实现一个队列。队列的声明如下,请实现它的两个函数appendTail
    // 和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能。
    
    #include "Queue.h"
    
    // ====================测试代码====================
    void Test(char actual, char expected)
    {
        if (actual == expected)
            printf("Test passed.
    ");
        else
            printf("Test failed.
    ");
    }
    
    int main(int argc, char* argv[])
    {
        CQueue<char> queue;
    
        queue.appendTail('a');
        queue.appendTail('b');
        queue.appendTail('c');
    
        char head = queue.deleteHead();
        Test(head, 'a');
    
        head = queue.deleteHead();
        Test(head, 'b');
    
        queue.appendTail('d');
        head = queue.deleteHead();
        Test(head, 'c');
    
        queue.appendTail('e');
        head = queue.deleteHead();
        Test(head, 'd');
    
        head = queue.deleteHead();
        Test(head, 'e');
    
        return 0;
    }
    测试代码

    分析:分析实际案例总结规律。

    牛客网给出了数据类型,不知道函数直接这样写到类定义内是否正确。

    class Solution
    {
    public:
        void push(int node) {
            stack1.push(node);
        }
    
        int pop() {
            if (stack2.size() <= 0)
            {
                while (stack1.size() > 0)
                {
                    int data = stack1.top();
                    stack1.pop();
                    stack2.push(data);
                }
            }
            
            if (stack2.size() == 0)
                ;
                //throw new exception("Empty queue");
            
            int head = stack2.top();
            stack2.pop();
            return head;
        }
    
    private:
        stack<int> stack1;
        stack<int> stack2;
    };
    牛客网提交代码

    相关题目:思路几乎一致,

    栈的弹出操作:队列1删除头节点并保存到队列2直到队列为空,最后的头节点为top值。

    栈的写入操作:正常压入到当前非空队列尾部。

  • 相关阅读:
    (第二天)原型、继承
    (第一天)包装对象、作用域、创建对象
    你欺骗了我,可选参数必须位于所有参数最后
    反射之动态创建对象
    异步编程
    前端性能优化方法
    性能瓶颈分析方法
    性能测试应用领域
    <转>jmeter(十五)函数助手
    正则表达式基础知识
  • 原文地址:https://www.cnblogs.com/ZSY-blog/p/12546599.html
Copyright © 2011-2022 走看看