typedef struct Stack_Node
{
ElemType data ;
struct Stack_Node *next ;
} Stack_Node ;
(1) 栈的初始化
Stack_Node *Init_Link_Stack(void)
{
Stack_Node *top ;
top=(Stack_Node *)malloc(sizeof(Stack_Node )) ;
top->next=NULL ;
return(top) ;
}
(2) 压栈(元素进栈)
Status push(Stack_Node *top , ElemType e)
{
Stack_Node *p ;
p=(Stack_Node *)malloc(sizeof(Stack_Node)) ;
if (!p) return ERROR;
/* 申请新结点失败,返回错误标志 */
p->data=e ;
p->next=top->next ;
top->next=p ; /* 钩链 */
return OK;
}
(3) 弹栈(元素出栈)
Status pop(Stack_Node *top , ElemType *e)
/* 将栈顶元素出栈 */
{
Stack_Node *p ;
ElemType e ;
if (top->next==NULL ) return ERROR ; /* 栈空,返回错误标志 */
p=top->next ;
e=p->data ; /* 取栈顶元素 */
top->next=p->next ; /* 修改栈顶指针 */
free(p) ;
return OK ;
}