当前位置: 代码迷 >> C语言 >> [分享]一个反向输出出入的程序
  详细解决方案

[分享]一个反向输出出入的程序

热度:243   发布时间:2007-04-18 13:11:38.0
[分享]一个反向输出出入的程序
#include <stdio.h>
#include <malloc.h>
#define NULL 0
typedef struct getchar /*difine a struct used to linklist*/
{
char data;
struct getchar *next;
}node;
node *creat(void) /*define a function used to order print*/
{
node *head,*p,*s;
char x;
int cycle=1;
head=(node *)malloc(sizeof(node));
p=head;
while(cycle)
{
scanf("%c",&x);
if(x!='0')
{
s=(node *)malloc(sizeof(node));
s->data=x;
p->next=s;
p=s;
}
else
cycle=0;
}
p->next=NULL;
p=head;
head=head->next;
free(p);
return head;
}
node *creatback(void) /*define a function used to reverse order print*/
{
node *back,*p;
char x;
int cycle=1;
back=(node *)malloc(sizeof(node));
back->data=NULL;
back->next=NULL;
while(cycle)
{
scanf("%c",&x);
if(x!='0')
{
p=(node *)malloc(sizeof(node));
p->data=x;
p->next=back;
back=p;
}
else
cycle=0;
}
free(p);
return back;
}
int main() /*function main*/
{
node *h,*he;
printf("Please input a string,ends with 0:\n");
h=creat();
printf("order:\n");
while(h!=NULL)
{
printf("%c",h->data);
h=h->next;
}
printf("\n");
printf("Please input a string,ends with 0:\n");
he=creatback();
printf("reverse order:\n");
while(he!=NULL)
{
printf("%c",he->data);
he=he->next;
}
printf("\nOVer!");
getch();
return 0;
}
大家给看看还有没有更好的方法!
搜索更多相关的解决方案: 输出  分享  

----------------解决方案--------------------------------------------------------
看不懂 ......
请你用中文注释下好么?
----------------解决方案--------------------------------------------------------
顺序输出可以用一般线型链表,反序输入可以用到栈。
估计楼主是这样的吧。
----------------解决方案--------------------------------------------------------
  相关解决方案