顺序栈的基本操作

#include<stdio.h>
#define MaxSize 10
typedef char ElemType;
typedef struct
{
	ElemType data[MaxSize];
	int top;
}SqStack;
void InitStack(SqStack &s)			//初始化栈
{
	s.top=-1;
}
bool Push(SqStack &s,ElemType e)		//进栈
{
	if(s.top==MaxSize-1)		//判满
		return false;
	s.data[++s.top]=e;			//指针加1,再进栈
	return true;
}
bool Pop(SqStack &s,ElemType &e)		//出栈
{
	if(s.top==-1)
		return false;
	e=s.data[s.top--];					//先出栈,指针再减1
	return true;
}
bool GetTop(SqStack s,ElemType &e)		//读取栈顶元素
{
	if(s.top==-1)
		return false;
	e=s.data[s.top];
	return e;
}
void main()
{
	SqStack s;
	InitStack(s);
	ElemType e;
	Push(s,'a');
	Push(s,'b');
	Push(s,'c');
	GetTop(s,e);
	printf("Top1=%c
",e);
	Pop(s,e);
	GetTop(s,e);
	printf("Top2=%c
",e);
}

  

原文地址:https://www.cnblogs.com/-slz-2/p/13195739.html