#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 100
typedef struct
{
int data[MAX];
int top;
}seqstack;
seqstack *built_stack()
{
seqstack *s;
s=malloc(sizeof(seqstack));
s->top=-1;
return s;
}
Push_seqstack(seqstack *s,int x)
{
if(s->top==MAX-1)
printf("\n.");
else
{
s->top++;
s->data[s->top]=x;
printf("成功添加一个数据\n");
}
}
Pop_seqstack(seqstack *s)
{
if(s->top==-1)
printf("对不起栈里为空\n");
else
{
printf("现在输出一个数据%d\n.",s->data[s->top]);
s->top--;
}
}
Pop_allseqstack(seqstack *s)
{
if(s->top==-1)
printf("对不起栈为空\n.");
else
{
while(s->top>=0)
{
printf("-> %d ",s->data[s->top]);
s->top--;
}
}
}
Search_seqstack(seqstack *s,int x)
{
if(s->top==-1)
{
printf("对不起栈为空\n");
return 0;
}
else
{
while(s->top>=0)
{
if(s->data[s->top]==x)
{ x=s->top+1;
printf("\n\t您要找的数据在堆栈中,处于第%d个空间.",x);
return 1;
}
else
s->top--;
}
printf("\n\t很抱歉,您要找的数据不在堆栈中.");
return 0;
}
}
Clear_seqstack(seqstack *s)
{
s->top=-1;
printf("现在栈已经为空\n.");
}
main()
{
char selected;
seqstack *seq;
seq=built_stack();
int x;
while(1)
{
printf("1为输入数据.\n");
printf("2为输出一个数据.\n");
printf("3为输出全部的数据.\n");
printf("4为查找你所需要的数据.\n");
printf("5为清空栈.\n");
if(selected == '0')
break;
switch(selected)
{
case'1':
printf("请输入一个数据\n");
scanf("%d",&x);
int Push_seqstack(seqstack *s,int x);
getch();
break;
case'2':
int Pop_seqstack(seqstack *s);
getch();
break;
case'3':
int Pop_allseqstack(seqstack *s);
getch();
break;
case'4':
printf("请输入你要查询的数据%d",&x);
int Search_seqstack(seqstack *s,int x);
getch();
break;
case'5':
int Clear_seqstack(seqstack *s);
getch();
break;
}
}
}