链栈的操作
程序员文章站
2022-03-10 11:45:55
...
链栈的定义:
typedef struct StackNode
{
SElemType data;
struct StackNode *next;
} StackNode, *LinkStackPtr;
typedef struct LinkStack
{
LinkStackPtr top;
int count;
} LinkStack;
链栈的进栈操作:
/* 插入元素e为新的栈顶元素 */
Status Push(LinkStack *S, SElemType e)
{
LinkStackPtr s = (LinkStackPtr) malloc( sizeof(StackNode));
s->data = e;
s->next = S->top;
S-top = s;
S->count++;
return OK;
}
链栈的出栈操作
/* 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回 Error */
Status Pop( LinkStack *S, SElemType *e)
{
LinkStackPtr p;
if ( StackEmpty (*S)) return Error;
*e = S-top->data;
p = S-top;
S-top = S->top->next;
free§;
S->count–;
return OK;
}
上一篇: 7-9 求整数均值 (10 分)
下一篇: 汇编语言 实验10.3 数值显示