//-----------------------------------
//Stack.h
//-----------------------------------
const int SIZE =10;
//-----------------------------------
class Stack{
int stck[SIZE]; // holds the stack
int tos // index of top-of-stack
public:
Stack():tos(0){}
int top() const;
void push(int ch); // push integer to stack
void pop(); // pop integer from stack
};//---------------------------------
//----------------------------------- //Stack.h //----------------------------------- const int SIZE =10; //----------------------------------- class Stack{ int stck[SIZE]; // holds the stack int tos // index of top-of-stack public: Stack():tos(0){} int top() const; void push(int ch); // push integer to stack void pop(); // pop integer from stack };//---------------------------------//----------------------------------- //Stack.cpp //----------------------------------- #include"Stack.h" #include<iostream> using namespace std; //----------------------------------- int Stack::top()const{ if(tos==0){ std::cout<<"\nStack is emty\n"; return 0; //return null on empty stack } return stck[tos-1]; }//---------------------------------- void Stack::push(int ch){ if(tos==SIZE){ std::cout<<"\nStack is full\n"; return } stck[tos++]=ch; }//---------------------------------- void Stack::pop(){ if(tos==0){ std::cout<<"\nStack is empty\n"; return; } --tos; }//----------------------------------//----------------------------------- //EX0805.cpp //栈应用 //----------------------------------- #include"Stack.h" #include<iostream> using namespace std; //----------------------------------- int main() { Stack s; s.push(10); s.push(12); s.push(14); std::cout<<s.top()<<"\n"; s.pop(); std::cout<<s.top()<<"\n"; }//----------------------------------