栈的链式存储结构及实现

//栈的链式存储结构及实现
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复制给栈顶指针
	S->count++;
	return OK;
}

//删除s的栈顶元素,用e返回其值
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 (p);
	S->count--;
	return OK;
}


你可能感兴趣的:(struct,存储)