C++ 简单实现压栈出栈

2015-07-20 17:05:37 · 作者: · 浏览: 3

?

?

/**********************************************************************  
 * Copyright (c)2015,WK Studios
 * Filename:  stack.h  
 * Compiler: GCC,VS,VC6.0  win32  
 * Author:WK  
 * Time: 2015 3 29
 ************************************************************************/ 
#include
  
   
using 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<
    
     

?