[分享]一个反向输出出入的程序
#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;
}
大家给看看还有没有更好的方法!
#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;
}