首页 > 其他 > 详细

顺序栈的基本操作

时间:2020-06-26 19:02:38      阅读:48      评论:0      收藏:0      [点我收藏+]
#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\n",e);
	Pop(s,e);
	GetTop(s,e);
	printf("Top2=%c\n",e);
}

  

顺序栈的基本操作

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!