#include<stdio.h>
#include<malloc.h>//内存分配
#include<string.h>
{
int element[size];
int top;
}seqStack;
//initial seqStack
void init(seqStack *s)
{
s->top=-1;//初始化栈
}
/
int pushs(seqStack *s,int x)
{
if(s->top==size-1)//入栈时需要判栈满
return -1;
s->top++;
s->element[s->top]=x;
return 1;
}
int pops(seqStack *s,int *x)
{
if(s->top==-1)
{
printf("stack is full!");
return 0;//false
}
*x=s->element[s->top];//出栈时只能取栈顶元素
s->top--;
return 1;//true
}
int readTop(seqStack *s,int *x)
{
if(s->top==-1)
{
printf("stack is empty!");
return 0;
}
*x=s->element[s->top];
return 1;
}
void outPut(seqStack *s)
{
int i;
for(i=0;i<=s->top;i++)
{
printf("%5d",s->element[i]);
}
printf("\n");
}
int main()
{
seqStack *s;
int e;
char c;
s=malloc(sizeof(seqStack));
init(s);
printf("输入入栈的数字:");
scanf("%d",&e);
pushs(s,e);
printf("出栈的数字:");
outPut(s);
pops(s,&e);
free(s);
return 0;
}
#include<malloc.h>//内存分配
#include<string.h>
#include<ctype.h>//该头文件中包含大小写转换函数toupper(),tolower();
#define size 10
{
int element[size];
int top;
}seqStack;
//initial seqStack
void init(seqStack *s)
{
s->top=-1;//初始化栈
}
/
int pushs(seqStack *s,int x)
{
if(s->top==size-1)//入栈时需要判栈满
return -1;
s->top++;
s->element[s->top]=x;
return 1;
}
int pops(seqStack *s,int *x)
{
if(s->top==-1)
{
printf("stack is full!");
return 0;//false
}
*x=s->element[s->top];//出栈时只能取栈顶元素
s->top--;
return 1;//true
}
int readTop(seqStack *s,int *x)
{
if(s->top==-1)
{
printf("stack is empty!");
return 0;
}
*x=s->element[s->top];
return 1;
}
void outPut(seqStack *s)
{
int i;
for(i=0;i<=s->top;i++)
{
printf("%5d",s->element[i]);
}
printf("\n");
}
int main()
{
seqStack *s;
int e;
char c;
s=malloc(sizeof(seqStack));
init(s);
printf("输入入栈的数字:");
scanf("%d",&e);
pushs(s,e);
printf("出栈的数字:");
outPut(s);
pops(s,&e);
free(s);
return 0;
}