// 栈.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef char ElemType;
#define MAX_INIT_SIZE 100
struct SeqStack{
ElemType data[MAX_INIT_SIZE];
int top;
};
//初始化栈
SeqStack *InitStack()
{
SeqStack *stack=new SeqStack;
stack->top=-1; //置空栈的运算就是将stack->top置为-1;
return stack;
}
//判断是否为空
bool StackEmpty(SeqStack *stack)
{
if(stack->top>=0)
return false;
else
return true;
}
//判断是否已满
bool StackFull(SeqStack *stack)
{
if(stack->top<MAX_INIT_SIZE-1&&stack->top>=0)
return false;
else
return true;
}
//push
void Push(SeqStack *stack,ElemType e)
{
//始终记得要对栈的空 满进行判断
if (stack->top>=-1&&stack->top<MAX_INIT_SIZE)
{
stack->top++;
stack->data[stack->top]=e;
}
else
cout<<"error"<<endl;
}
//Pop
void Pop(SeqStack *stack,ElemType &e)
{
if (stack->top>=0) //top =0 是第一个储存位置
{
e=stack->data[stack->top];
stack->top--;
}
else
cout<<"error"<<endl;
}
int main(int argc, char* argv[])
{
cout<<"初始化栈!"<<endl;
SeqStack *S;
S=InitStack();
cout<<"请输入入栈元素,并按<ENTER>结束:"<<endl;
ElemType ch;
ch=getchar();
while(ch!='\n')
{
Push(S,ch);
ch=getchar();
}
while(S->top>=0)
{
Pop(S,ch);
cout<<ch<<" ";
}
cout<<endl;
return 0;
}