#include<stdio.h>
#include<malloc.h>
#define ElemType int
typedef struct LinkNode{//定义栈中节点类型
ElemType data;
struct LinkNode *next;
}LinkNode;
typedef struct Stack{//定义栈的结构
LinkNode *top;//栈顶指针
int stacksize;//栈的大小
}LinkStack;
LinkStack Init_stack(LinkStack &S){
//LinkStack S;
S.top=NULL;//初始化栈
S.stacksize=0;
return S;
}
LinkStack Push_stack(LinkStack &S){
LinkNode *p;
ElemType a;
p=(LinkNode *)malloc(sizeof(LinkNode));
if(p==NULL){
printf("上溢");
}else{
printf("输入入栈元素-1为退出输入
");
scanf("%d",&a);
while(a!=-1){
p->data=a;
p->next=S.top;
S.top=p;
S.stacksize++;
scanf("%d",&a);
}
}
return S;
}
void Pop_stack(LinkStack &S){
LinkNode *p;
int a;
if(S.top==NULL){
printf("下溢");
}
else{
p=S.top;
a=S.top->data;
S.top=S.top->next;
printf("栈顶元素出栈:%d
",a);
free(p);
printf("%d",S.top->data);
S.stacksize--;
}
}
void Get_top(LinkStack &S){
ElemType a;
if(S.top==NULL){
printf("空栈");
}
printf("
取栈顶元素:");
printf("%d
",S.top->data);
}
int main(){
LinkStack S;
Init_stack(S);
Push_stack(S);
//Get_top(S);
Pop_stack(S);
// Pop_stack(S);
//Get_top(S);
return 0;
}