欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

链栈的操作

程序员文章站 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;
}

相关标签: 摘录 C