栈_数列反转

来源:互联网 发布:淘宝备注在哪 编辑:程序博客网 时间:2024/06/05 23:50

栈_数列反转

#include<iostream>#include<string>#include<cassert>using namespace std;template<class Type> class Stack {private:    Type *urls;    int max_size, top_index;public:    Stack(int length_input) {        urls = new Type[length_input];        max_size = length_input;        top_index = -1;    }    ~Stack() {        delete[] urls;    }    bool push(const Type &element) {        if (top_index >= max_size - 1) {            return false;        }        top_index++;        urls[top_index] = element;        return true;    }    bool pop() {        if (top_index < 0) {            return false;        }        top_index--;        return true;    }    Type top() {        assert(top_index >= 0);        return urls[top_index];    }    bool empty(){        if( top_index<0){            return true;        }        else{            return false;        }    }};int main() {    //n表示输入元素个数    int n,num;    cin>>n;    Stack<int> stack(n);  //定义一个int类型的栈    for(int i=1;i<=n;i++){        cin>>num;        stack.push(num);    }    while(!stack.empty()){        cout<<stack.top()<<" ";        stack.pop();    }    return 0;}
0 0