#include <stdio.h>
#include <stdlib.h>
typedef struct LinkNode{
int data;
struct LinkNode* next;
}LinkStack;
LinkStack *InstLinkStack()
{
LinkStack* S = (LinkStack*)malloc(sizeof(LinkStack));
S->next = NULL;
return S;
}
void StackEmpty(LinkStack * S)
{
LinkStack * P = S;
if(P->next == NULL)
{
printf("链栈为空!\n");
}
else
{
printf("链栈不为空!\n");
}
}
void Push(LinkStack * S,int x)
{
LinkStack * P = S;
LinkStack * Q = (LinkStack*)malloc(sizeof(LinkStack));
Q->data = x;
Q->next = P->next;
P->next = Q;
}
void Pop(LinkStack *S)
{
LinkStack * P = S;
if(P->next == NULL)
{
printf("链栈为空!\n");
return;
}
while (P->next != NULL){
LinkStack * Q = P->next;
printf("出栈元素为:%d\n",Q->data);
P->next = Q->next;
}
}
int GetTop(LinkStack* S)
{
LinkStack * P = S;
if(P->next == NULL)
{
printf("链栈是空栈\n");
exit(1);
}
P = P->next;
int e = P->data;
return e;
}
int main()
{
LinkStack* S = InstLinkStack();
StackEmpty(S);
Push(S,5);
Push(S,4);
Pop(S);
Push(S,3);
Push(S,2);
Push(S,1);
StackEmpty(S);
Pop(S);
printf("栈顶元素为:%d\n",GetTop(S));
return 0;
}