zoukankan      html  css  js  c++  java
  • 栈(模板)

    #include <iostream>
    
    using namespace std;
    
    class Stack
    {
    public:
        Stack();//初始化
        Stack(int _size);
        ~Stack();
        bool empty();  //栈是否为空
        int top();     //获取栈顶元素
        bool pop();    //弹栈
        bool push(int element);   //压栈
        int size();     //返回栈中元素的个数
    
    private:
        int *data;
        int top;
        int size;
    };
    
    Stack::Stack()
    {
        data = new int[100];
        top = -1;
        size = 100;
    }
    
    Stack::Stack(int _size)
    {
        data = new int[size];
        size = _size;
        top = -1;
    }
    
    Stack::~Stack()
    {
        delete []data;
    }
    
    bool Stack::empty()
    {
        return top == -1? true : false;
    }
    
    int Stack::top()
    {
        if(empty())
        {
            return ;
        }
        return data[top];
    }
    
    bool Stack::push(int element)
    {
        if(top == size - 1)
        {
            return false;
        }
        top ++;
        data[top] = element;
    }
    
    bool Stack::pop()
    {
        if(top > -1)
        {
            top --;
        }
    }
    
    int Stack::size()
    {
        return top+1;
    }
  • 相关阅读:
    7、shell函数
    5、shell分支
    6、shell循环
    4、shell中的test命令
    3、shell中引号
    2、shell变量
    1、建立和运行shell
    awk命令简介
    18、异步IO
    Python模块:sys
  • 原文地址:https://www.cnblogs.com/topk/p/6580105.html
Copyright © 2011-2022 走看看