标签:
/**********************************************************************
* Copyright (c)2015,WK Studios
* Filename: stack.h
* Compiler: GCC,VS,VC6.0 win32
* Author:WK
* Time: 2015 3 29
************************************************************************/
#include<iostream>
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<<stck[i]<<' ';
}
cout<<'\n';
}
/**********************************************************************
* 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<<endl;//换行的同时刷新缓冲区
Stack ss; //定义对象
int x=0;
char ch;
cout<<" <I>----- push data to Stack!\n";
cout<<" <O>----- pop data from Stack!\n";
cout<<" <S>----- show content of Stack!\n";
cout<<" <Q>----- Quit !!!!!!!\n";
while(1)
{
cout<<"Please select an item:";
cin>>ch;
ch=toupper(ch);
switch(ch)
{
case 'I':
cout<<"Enter the value that you want to push:";
cin>>x;
ss.push(ch);
break;
case 'O':
x=ss.pop();
cout<<"pop "<<x<<" from Stack!\n";
break;
case 'S':
ss.ShowStack();
break;
case 'Q':
return 0;
break;
default :
cout<<"You have iputted a wrong item!!!! Please try again !\n";
continue;
}
}
}标签:
原文地址:http://blog.csdn.net/kai8wei/article/details/44906879