数据结构 栈的逆转
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define MAXSIZE 1024
typedef struct
{
int data[MAXSIZE];
int top;
}seqstack;
seqstack *st;
seqstack * init_stack()
{
st=(seqstack*)malloc(sizeof(seqstack));
st->top=-1;
return st;
}
int empty_stack(seqstack *st)
{
return((st->top==-1)?1:0);
}
void pop(seqstack *st,int *x)
{
*x=st->data[st->top];
st->top--;
}
int push(seqstack *st,int x)
{
while(!empty_stack(st))
{
x=st->data[st->top];
st->top--;
}
return 1;
}
void insert_stack(seqstack *st,int x)
{
if(st->top==MAXSIZE-1)
exit(1);
st->data[st->top+1]=x;
st->top++;
}
void print_stack(seqstack *st)
{
while(!empty_stack(st))
{printf("%d",st->data[st->top]);
st->top--;
}
}
int main()
{
seqstack *st,*st1,*st2;
int i=1;
int y,n;
st=init_stack();
st1=init_stack();
st2=init_stack();
printf("please input the number\n");/*输入数字个数*/
scanf("%d",&n);
while(i<=n)
{
scanf("%d",&y);
i++;
insert_stack(st,y);
}
while(!empty_stack(st))
{
pop(st,&y);
push(st1,y);
}
print_stack(st1);
while(!empty_stack(st1))
{
pop(st1,&y);
push(st2,y);
}
while(!empty_stack(st2))
{
pop(st2,&y);
push(st,y);
}
return 0;
}
编写一个算法,利用栈的基本运算将指定栈中的内容进行逆转。
现在无法正确输出结果,求大神帮忙看看,哪儿有问题。
[解决办法]
push函数写错了吧,应该和你的 insert_stack是一样的吧