c++类实现栈

    科技2024-08-22  27

    #include<iostream> using namespace std; class Stack{ private: int* data; int top; int base; int size; public: Stack(); Stack(int); ~Stack(); int InitStack(); int getTop(int &); int isEmpty(); int isFull(); int Push(int); int Pop(int &); int Clear(); int Print(); }; Stack::Stack(){ size=100; InitStack(); } Stack::Stack(int Size){ this->size=Size; InitStack(); } Stack::~Stack(){ delete data; } int Stack::InitStack(){ top=base=-1; data=new int[size]; } int Stack::getTop(int &e){ e=data[top]; } int Stack::isEmpty(){ return (top==-1)?0:-1; } int Stack::isFull(){ return (top==size)?0:-1; } int Stack::Push(int e){ if(top<size) data[++top]=e; else return -1; } int Stack::Pop(int &e){ if(top!=base) e=data[top--]; else return -1; } int Stack::Clear(){ top=-1; return 0; } int Stack::Print(){ if(top==base) return -1; else{ int p=base; while(p!=top){ cout<<data[++p]<<" "; } } } int main(){ Stack s; s.Push(11); s.Push(22); s.Print(); }
    Processed: 0.014, SQL: 8