栈 数组

来源:互联网 发布:百度云盘mac版下载 编辑:程序博客网 时间:2024/05/24 04:29
#include <iostream>using namespace std;typedef int T;class Stack{T a[10];int num;//记录已经放入的元素个数 public:void push(const T& t){if (full())throw "stack overflow";a[num++] = t;}void pop(){if (empty())throw "empty stack";num--;}T top(){if (empty())throw "no top stack";return a[num-1];} bool empty() { return num == 0;    }    bool full()    {    return num == 10;    }int size(){return num;} int capacity(){return 10;}void clear(){num = 0;}Stack():num(0){}};int main(){Stack s;s.push(1);s.push(2);s.push(3);/*for (int i=0; i<10; i++){s.push(i*5);}*/while(!s.empty()){cout << s.top() << ' ';s.pop();}cout << endl;/*for (int i=0; i<10; i++)s.push(i);cout << "full?" << s.full() << endl;s.push(100); //there is  an exceptioncout << "cant see this" << endl;*/}