#include "StdAfx.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int DataType;
struct Node
{
DataType data;
struct Node* next;
};
typedef struct Node *PNode;
typedef struct Node *top;
typedef struct Node *LinkStack;
LinkStack SetNullStack()
{
LinkStack top=(LinkStack)malloc(sizeof(struct Node));
if(top!=NULL)
top->next =NULL;
else
printf("alloc failure");
return top;
}
int IsNullStack(LinkStack top)
{
return(top->next == NULL);
}
void Push(LinkStack top,DataType x)
{
PNode p=(PNode)malloc(sizeof(struct Node));
if(p!=NULL)
{
p->data =x;
p->next =top->next ;
top->next =p;
}
else
printf("alloc failure!\n");
}
void Pop(LinkStack top)
{
PNode p;
if(IsNullStack(top))
printf("empty!\n");
else
{
p=top->next ;
top->next =p->next ;
free(p);
}
}
int top_seq(LinkStack top)
{
if(IsNullStack(top))
printf("empty!\n");
else
return top->next ->data ;
}
void printLinkStack(LinkStack top)
{
PNode p;
if (top == NULL)
printf("\n The Linklist is NULL !\n");
p = top->next;
while (p != NULL)
{
printf("%d", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
LinkStack stack1=SetNullStack();
Push(stack1,5);
Push(stack1,8);
Push(stack1,9);
printLinkStack(stack1);
Pop(stack1);
printLinkStack(stack1);
printf("the top of the stack is %d\n",top_seq(stack1));
printLinkStack(stack1);
system("pause");
}