数据结构共享栈 问题好像是入栈入不进去 求指导 多谢
发布网友
发布时间:2022-06-02 00:22
我来回答
共1个回答
热心网友
时间:2023-10-22 05:07
希望如下对你有用:
/*栈的基本操作*/
# 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);
}