zoukankan      html  css  js  c++  java
  • C++学习笔记49:栈

    栈是一种只能从一端访问的线性数据结构,栈是一种后进先出的数据结构

    //stack.h
    #ifndef STACK_H
    #define STACK_H
    #include <cassert>
    template <class T, int SIZE = 50> class Stack
    {
    private:
        T list[SIZE];
        int top;
    public:
        Stack();
        void push(const T &item);
        T pop();
        void clear();
        const T &peek() const;
        bool isEmpty() const;
        bool isFull() const;
    };
    
    //模板的实现
    template <class T, int SIZE>
    Stack<T, SIZE>::Stack():top(-1){}
    
    template <class T, int SIZE> void Stack<T, SIZE>::push(const T &item)
    {
        assert(!isFull());
        list[++top] = item;
    }
    
    template <class T, int SIZE> T Stack<T, SIZE>::pop() const
    {
        assert(!isEmpty());
        return list[top--];
    }
    
    template <class T, int SIZE> const T &Stack<T, SIZE>::peek() const
    {
        assert(!isEmpty());
        return list[top];
    }
    
    template <class T, int SIZE> bool Stack<T, SIZE>::isEmpty() const
    {
        return top == -1;
    }
    
    template <class T, int SIZE> bool Stack<T, SIZE>::isFull() const
    {
        return top == SIZE - 1;
    }
    
    template <class T, int SIZE> void Stack<T, SIZE>::clear()
    {
        top = -1;
    }
    
    #endif // 

     栈的引用:判断是否存在不匹配的()

    #include <iostream>
    #include <string>
    #include <cassert>
    using namespace std;
    
    template <class T, int MAX = 1000> class Stack
    {
    private:
        T list[MAX + 1];
        int top;
    public:
        Stack();
        void push(const T &item);
        T pop();
        const T & peek() const;
        bool isEmpty() const;
    };
    
    
    template <class T, int MAX> Stack<T, MAX>::Stack() :top(-1)
    {
    
    }
    
    template <class T, int MAX> void Stack<T, MAX>::push(const T &item)
    {
        if (top != MAX - 1)
        {
            list[++top] = item;
        }
    }
    
    template <class T, int MAX> bool Stack<T, MAX>::isEmpty() const
    {
        return top == -1;
    }
    
    template <class T, int MAX> T Stack<T, MAX>::pop()
    {
        assert(!isEmpty());
        return list[top--];
    }
    
    template <class T, int MAX> const T &Stack<T, MAX>::peek() const
    {
        assert(!isEmpty());
        return list[top];
    }
    
    void judgeBrackets(string str)
    {
        Stack<char>p;
        int k = 0;
        int count = 0;
        while (str[k] != '')
        {
            if (str[k] == '(')
            {
                p.push(str[k]);
                count++;
            }
            if (str[k] == ')')
            {
                if (count == 0)
                {
                    cout << "NO" << endl;
                    count--;
                    break;
                }
                else
                {
                    p.pop();
                    count--;
                }
            }
            k++;
        }
        if (count == 0)
            cout << "Yes" << endl;
        if (count > 0)
            cout << "NO" << endl;
    }
    
    int main()
    {
        string str;
        while (cin >> str)
        {
            judgeBrackets(str);
        }
    }
    怕什么真理无穷,进一寸有一寸的欢喜。---胡适
  • 相关阅读:
    Struts2获取参数的几种方式
    Struts2的Action中访问servletAPI方式
    struts2中常用配置
    struts2发送ajax的几个问题(不使用struts2-json-plugin的情况下)
    深入Struts2的过滤器FilterDispatcher--中文乱码及字符编码过滤器
    Ironic 裸金属实例的部署流程
    Ironic 裸金属管理服务的底层技术支撑
    Cinder AZ 与 Nova AZ 的同步问题
    OpenStack 对接 Ceph 环境可以创建卷但不能挂载卷的问题
    OpenStack 节点重启后无法联网的问题
  • 原文地址:https://www.cnblogs.com/hujianglang/p/6556836.html
Copyright © 2011-2022 走看看