首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

数据结构 栈的逆转解决办法

2012-10-06 
数据结构 栈的逆转#includestdio.h#includestdlib.h#includemalloc.h#define MAXSIZE 1024typedef s

数据结构 栈的逆转
#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是一样的吧

热点排行