#ifndef STATUS_H
#define STATUS_H
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int SElemType;
#endif
```cpp
#ifndef STACK_H
#define STACK_H
#include <stdio.h>
#include "status.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct Node{
struct Node *next;
SElemType date;
}stack;
typedef struct Stack{
stack *top;
int length;
}SqStack;
Status InitStack(SqStack *s);
Status Push(SqStack *s,SElemType e);
Status Pop(SqStack *s,SElemType *e);
Status GetTop(SqStack *s,SElemType *e);
Status StackEmpty(SqStack *s);
#endif
```cpp
#include <stdio.h>
#include "status.h"
#include "stack.h"
Status InitStack(SqStack *s){
if(s==NULL){
return ERROR;
}
s->top=NULL;
s->length=0;
return OK;
}
Status Push(SqStack *s,SElemType e){
if(s==NULL){
return ERROR;
}
stack *p=(stack*)malloc(sizeof(stack));
p->next=s->top;
p->date=e;
s->top=p;
s->length++;
return OK;
}
Status Pop(SqStack *s,SElemType *e){
if(s==NULL&&s->length){
return ERROR;
}
stack *p=s->top;
*e=p->next->date;
s->top=p->next;
s->length--;
return OK;
}
Status GetTop(SqStack *s,SElemType *e){
stack *p=s->top;
*e=p->date;
return OK;
}
Status StackEmpty(SqStack *s){
if(s==NULL)
return TRUE;
else
return FALSE;
}
```cpp
#include <stdio.h>
#include "stack.h"
int main(){
SqStack *s;
SqStack sq;
int n,i,t;
SElemType e;
s=&sq;
InitStack(s);
printf("当前长度为%d\n",s->length);
printf("请输入你想要输入数的个数\n");
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("你输入的第%d个数为:",i);
scanf("%d",&e);
Push(s,e);
}
printf("\n");
t=Push(s,e);
if(t==OK)
printf("链栈不为空\n");
else
printf("链栈为空\n");
printf("\n");
GetTop(s,&e);
printf("栈顶元素为:%d\n",e);
printf("\n");
t=StackEmpty(s);
if(t==TRUE)
printf("栈链表为空\n");
else
printf("栈链表不为空\n");
printf("\n");
printf("栈链表依次出栈\n");
while(s!=NULL){
Pop(s,&e);
printf("%d \n",e);
}
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200510180026442.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0phdmFwcmludGY=,size_16,color_FFFFFF,t_70)