链栈的类型:
<pre name="code" class="java">typedef struct stacknode{
DataType data
struct stacknode *next
}StackNode;
typedef struct{
StackNode *top;//栈顶指针
}LinkStack;
链表的基本运算:
<pre name="code" class="java">public class LinkStack {
//(1)置栈空
void InitStack(LinkStack *S){
S->top = null;
}
//(2)判断栈空
int StackEmpty(LinkStack *S){
return S->top == null;
}
//(3)进栈
void push(LinkStack *S,DataType x){
//将元素x插入链栈头部
StackNode *p = (StackNode *)malloc(sizeof(StackNode));
}
//(4)退栈
DataType Pop(LinkStack *S){
DataType x;
StackNode *p = S->top;//保存栈顶指针;
if(StackEmpty(S)){
Error("Stack underflow");//下溢
}
x = p->data;
S->top = p->next;//将栈顶指针下移
free(p);
return x;
}
//(5)取栈顶元素
DataType Pop(LinkStack *S){
if(StackEmpty(S)){
Error("Stack is empty");
}
return S->top->data;
}
}