#include <stdio.h>
#include <stack>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int SElemType;
typedef struct StackNode{
ElemType data;
struct StackNode* next;
}StackNode,*LinkStack;
int InitStack(LinkStack &S){
S = NULL;
return OK;
}
int Push(LinkStack &S, ElemType e){
StackNode* p;
p = new StackNode;
p->data = e;
p->next = S->next;
S = p;
return OK;
}
int Pop(LinkStack &S){
StackNode* p;
if(S == NULL){
return ERROR;
}
p = S;
S = S->next;
delete p;
return OK;
}
int GetTop(LinkStack S){
if(S != NULL){
return S->data;
}
}
void Print(LinkStack S)
{
StackNode *p;
p = S;
while (p == NULL)
{
printf("%d ",(p->data));
p = p->next;
}
printf("\n");
}
int main(void){
LinkStack S;
int x,y;
InitStack(S);
printf("Enter Numbers:\n");
while(true){
scanf("%d",&x);
if(x == 9999){
break;
}
Push(S,x);
}
printf("The stack elems:");
Print(S);
printf("1.入栈:");
scanf("%d",&x);
Push(S,x);
printf("The stack elems:");
Print(S);
y = GetTop(S);
printf("2.取得栈顶元素:%d\n",y);
printf("3.出栈:\n");
Pop(S);
printf("The stack elems:");
Print(S);
}