数据结构 用C语言编程实现进栈出栈

如题所述

第1个回答  推荐于2017-09-08
希望如下对你有用:
/*栈的基本操作*/
# define stacksize 100 /*定义栈的最大存储空间*/
# define LEN sizeof(struct stack)
static size=0;
struct stack {
int data;
int *top[stacksize];
};
struct stack *sqstack;
struct stack *s;
static e;

int push() /*将元素压入栈*/
{
if (size<=stacksize)
* sqstack->top[size++]=e;
else
printf (" the stack is full:");
}

int pop(struct stack *sqstack,int location) /*元素出栈*/
{
e=*(sqstack->top[location]);
return (e);
}

main()
{ int n,i,t,x=0;
int element;
printf ( "\n create the stack first :");
scanf ("%d ",&n);
for (i=1;i<=n;i++)
{
scanf ("%d",&e);
push (e);
}
s=sqstack;
t=size;
printf ("\n after pushed , the sqstack is :");
while (t>=0)
{
*s->top[t]=*sqstack->top[t];
t--;
}
t=size;
while (t!=0)
{
t--;
e=pop(s,t);
printf (" %d->",e);

}

printf ("\n which element you want to pop :");
scanf ("%d",&element);
while (size!=0)
{
e=pop(sqstack,size--);
if (element==e)
{
printf ("\n %d is poped",element);
x=1;
}
}
if(x==0)
printf ("\n %d is not found in the sqstack.\n",element);
}

本回答被提问者采纳
相似回答