LIFO 链栈结构

 typedef int ElemType;
struct node{
ElemType data;
struct node* next;
};
typedef struct node* Stack;

LIFO 链栈基本操作

 //LIFO 链栈初始化
void InitStack(Stack top){
top = NULL;
} //LIFO 链栈判断栈空
boolean StackKEmpty(Stack top){
if(top == NULL) return true;
else return false;
} //LIFO 链栈进栈
void Push(Stack top, ElemType x){
LinkedStack p;
p = malloc(sizeof *p);
p -> data =x;
p -> next = top;
top = p;
} //LIFO 链栈出栈
ElemType Pop(Stack top){
LinkedStack p;
ElemType x;
if(top == NULL){
printf("栈下溢错误!\n");
exit();
}
p = top;
x = p -> data;
top = top -> next;
free(p);
return x;
} //LIFO 链栈读取栈顶
ElemType GetTop(Stack top){
if(top == NULL){
printf("栈下溢错误! \n");
exit();
}
return top -> data;
}
05-11 22:23