/********************************************************************** * Copyright (c)2015,WK Studios * Filename: stack.h * Compiler: GCC,VS,VC6.0 win32 * Author:WK * Time: 2015 3 29 ************************************************************************/ #includeusing namespace std; const int SIZE=10; class Stack { private: int stck[SIZE];//數組用於存放棧中數據 int tos; //棧頂位置(數組的下標) public: Stack(); void push(int ch); //函數聲明向棧中中壓入數據fuction int pop(); //聲明從堆棧中彈出數據fuction void ShowStack(); //聲明顯示堆棧數據function };
/********************************************************************** * Copyright (c)2015,WK Studios * Filename: stack.cpp * Compiler: GCC,VS,VC6.0 win32 * Author:WK * Time: 2015 3 29 ************************************************************************/ #include"stack.h" //構造函數,初始化棧的實現 Stack::Stack() { tos=0; stck[SIZE]=0; } //向棧中壓入數據函數的實現 void Stack::push(int ch) { if(tos==SIZE) { cout<<"Stack is full!\n"; return ; } stck[tos]=ch; tos++; cout<<"You have pushed a data into the Stack!\n"; } //從棧中彈出數據函數的實現 int Stack::pop() { if(0==tos) { cout<<"Stack is empty!\n"; return 0; } tos--; return stck[tos]; } //顯示棧中數據的函數的實現 void Stack::ShowStack() { cout<<"The content of Stack:\n"; if(0==tos) { cout<<"The Stack has no data!\n"; return ; } for(int i=tos-1;i>=0;i--) { cout< /********************************************************************** * Copyright (c)2015,WK Studios * Filename: main.cpp * Compiler: GCC,VS,VC6.0 win32 * Author:WK * Time: 2015 3 29 ************************************************************************/ #include"stack.h" int main() { cout<