const int StackMaxSize=30;
typedef int ElemType;
struct Stack{
ElemType stack[StackMaxSize];
int top;
};
void InitStack(Stack& s)
{
s.top=-1;
}
void ClearStack(Stack& s)
{
s.top=-1;
}
int StackEmpty(Stack& s)
{
return s.top==-1;
}
ElemType Peek(Stack& s)
{
if(s.top==-1)
{
cerr<<"Stack is empty!"<<endl;
exit(1);
}
return s.stack[s.top];
}
void Push(Stack& s,const ElemType& item)
{
if(s.top==StackMaxSize-1)
{
cerr<<"Stack overflow!"<<endl;
exit(1);
}
s.top++;
s.stack[s.top]=item;
}
ElemType Pop(Stack& s)
{
if(s.top==-1){
cerr<<"Stack is empty!"<<endl;
exit(1);
}
ElemType temp=s.stack[s.top];
s.top--;
return temp;
}
int StackFull(Stack& s)
{
return s.top==StackMaxSize-1;
}
//以下是stack.cpp
#include<iostream.h>//棧的簡單應用,從鍵盤上輸入一批整數,然後按照相反的次序打印出來
#include<stdlib.h>
#include"stack.h"
#include<conio.h>
void main()
{
Stack a;
InitStack(a);
int x;cout<<"輸入30個以下的整數,用-1作爲終止鍵盤輸入"<<endl;
cin>>x;
while(x!=-1){
//假定用-1作爲終止鍵盤輸入的標誌,輸入的整數個數不能超過StackMaxSize所規定的值
Push(a,x);
cin>>x;
}
while(!StackEmpty(a))
cout<<Pop(a)<<" ";
cout<<endl;
getch();
}
/*
http://f2.9612.org//vcpp/webinfo/WebInfoBata1.asp
QQ羣:
34409541 討論網頁
34409326 討論JAVA 已滿
34408784 討論VC++
34409699 討論VC++
9143041 討論MFC編程
10614204 討論C#
10613030 討論Win32編程
10613067 討論遊戲開發
18779860 討論JAVA
*/