/**************************栈的顺序存储结构*********************/
#include"stdio.h"
#define ERROR 0
#define OK 1
#define FALSE 0
#define TRUE 1
#define MAXSIZE 20
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType data[MAXSIZE];
int top;
}SqStack;
/*********************Operator***************************/
void InitStack(SqStack *s) //初始化栈
{
s->top=0;
}
void ClearStack(SqStack *s) //清空栈
{
s->top=0;
}
Status StackEmpty(SqStack s) //判断栈是否为空
{
if(0==s.top)
return TRUE;
return FALSE;
}
Status GetTop(SqStack s, ElemType *e) //取栈顶元素
{
if( StackEmpty(s) )
return FALSE;
*e=s.data[s.top];
return TRUE;
}
Status Push(SqStack *s,ElemType e) //入栈操作
{
if(s->top==MAXSIZE)
return FALSE;
s->data[s->top++]=e;
return TRUE;
}
Status Pop(SqStack *s,ElemType *e) //出栈操作
{
if(StackEmpty(*s))
return FALSE;
*e=s->data[s->top--];
return TRUE;
}
int StackLength(SqStack s) //返回栈的元素个数
{
return s.top;
}
main(void)
{
SqStack *s;
s=(SqStack*)malloc(sizeof(SqStack));
InitStack(&s);
}
数据结构之顺序存储的栈的实现
最新推荐文章于 2025-04-28 23:19:23 发布