#include <stdio.h>
#include "stack.h"
#include "status.h"
Status InitStack(SqStack *s){
s->elem=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(s->elem==NULL)
{
return ERROR;
}
s->length=0;
s->listsize=STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack *s,SElemType e){
if(s->length==s->listsize)
{
s->elem=(SElemType *)realloc(s->elem,(s->listsize+STACKINCREMENT)*sizeof(SElemType));
if(s->elem==NULL)
{
return ERROR;
}
}
s->elem[s->length]=e;
s->length++ ;
printf("当前长度验算%d \n",s->length);
return OK;
}
Status Pop(SqStack *s,SElemType *e){
*e=s->elem[s->length-1];
s->length--;
return OK;
}
Status GetTop(SqStack *s,SElemType *e){
int t;
t=s->length;
printf("当前长度为%d",t);
*e=s->elem[t-1];
return OK;
}
Status StackEmpty(SqStack *s){
printf("%d",s->length);
if(s->length==0){
return OK;
} else
return ERROR;
}
#include <stdio.h>
#include "stack.h"
int main(){
SqStack sq;
SElemType e;
int t,i,value,n;
SqStack *s=&sq;
InitStack(s);
printf("线性表的当前长度 :%d\n",s->length);
printf("线性表的当前分配的存储容量 :%d\n",s->listsize);
printf("向栈中插入元素\n");
printf("请输入插入的个数:");
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("请输入第%d个数为",i);
scanf("%d",&e);
Push(s,e);
}
if(t==ERROR){
printf("入栈异常\n");
} else{
printf("入栈成功\n");
}
printf("出栈运行\n");
printf("开始\n");
t=StackEmpty(s);
if(t==OK){
printf("为空\n");
}else
printf("不为空");
GetTop(s,&e);
printf("栈顶元素为%d\n",e);
for (;s->length;) {
Pop(s, &e);
printf("出栈结果%d \n", e);
}
}
#ifndef STACK_H
#define STACK_H
#include <stdio.h>
#include "status.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct {
SElemType *elem;
int length;
int listsize;
}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
#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
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200509075838718.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0phdmFwcmludGY=,size_16,color_FFFFFF,t_70#pic_center)