#include<stdio.h>
#include<stdlib.h>
//顺序栈练习
#define MaxSize 100
#define ElemType int
typedef struct {
ElemType data[MaxSize];
int top;
}SqStack;
//top初始值为-1,top总是指向栈顶,top=max-1即为栈满,top=-1即为栈空,进top++,出top--
//区分好栈指针和栈顶指针
void Init(SqStack *&s)
{
s = (SqStack *)malloc(sizeof(SqStack));
s->top = -1;
}
void Delete(SqStack *&s)
{
free(s);
}
bool Empty(SqStack *s)
{
return (s->top == -1);
}
bool Push(SqStack *&s,ElemType e)
{
if (s->top == MaxSize - 1)
return false;
s->top++;
s->data[s->top] = e;
return true;
}
bool Pop(SqStack *&s, ElemType &e)
{
if (s->top == -1)
return false;
e = s->data[s->top];
s->top--;
return true;
}//出栈,对栈进行改动
bool GetTop(SqStack *s, ElemType &e)
{
if (s->top == -1)
return false;
e = s->data[s->top];
return true;
}//
顺序栈定义练习
最新推荐文章于 2023-10-20 21:42:37 发布