栈
抽象数据类型描述
ADT Stack
{
数据对象:
D = {ai | 1 <= i <= n , n = 0 , ai为ElemType类型}
数据关系:
R = { < ai,ai + 1 | ai,ai + 1 属于D, i = 1,...,n - 1}
基本运算:
InitStack(&s) :初始化栈,构造一个空栈s;
DestroyStack(&s) :销毁栈,释放空间;
StackEmpty(s) :判空;
Push(&s, e) :进栈;
Pop(&s, &e) :出栈,返回删除的栈顶元素;
GetTop(s, &e) :取栈顶元素;
}
---------------------------------------------------------------------------------------------------------------------------------
定义
#define MAXSIZE 10000
typedef int ElemType;
typedef struct {
ElemType data[MAXSIZE];
int top;
}LinkStack;
初始化
void InitStack(LinkStack*& s)
{
s = (LinkStack*)malloc(sizeof(LinkStack));
s->top = -1;
}
销毁
void DestoyStack(LinkStack*& s)
{
free(s);
}
判空
bool StackEmpty(LinkStack* s)
{
return (s->top == -1);
}
进栈
bool Push(LinkStack*& s, ElemType e)
{
if (s->top == MAXSIZE - 1)
return false;
s->top++;
s->data[s->top] = e;
return true;
}
出栈
bool Pop(LinkStack*& s, ElemType& e)
{
if (s->top == -1)
return false;
e = s->data[s->top];
s->top--;
return true;
}
读取栈顶
bool GetTop(LinkStack* s,ElemType &e)
{
if (s->top == -1)
return false;
e = s->data[s->top];
return true;
}