//链栈的基本存储
//1.1链栈的存储结构
typedef struct StackNode
{
SElemType data;
struct StackNode* next;
}StackNode, * LinkStack;
//1.2链栈的初始化
Statue InitStack(LinkStack &S)
{//初始栈,将栈顶元素至空
S==null;
return true;
}
//1.3链栈的入栈操作
Status PushStack(LinkStack& S, SElemType e)
{ //在栈顶插入元素e,链栈不需要判断栈满
StackNode* p = new StackNode;
p->data = e;
p->next = S;
S = p;
return OK;
}
//1.4链栈的出栈操作
Status PopStack(LinkStack& S, SElemType& e)
{ //删除S的栈顶元素,用e返回其值
if (S == NULL) //判断是否为栈空
return ERROR;
e = S->data;
StackNode* p = S;
S = S->next;
delete p;
return OK;
}
//1.5取链栈的栈顶元素
SElemType GetTop(LinkStack S, SElemType &e)
{ //返回S的栈顶元素,不修改栈顶指针
if (S != NULL) //非栈空时返回
return S->data;
return ERROR;
}
05-10
2059
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)