#include<stdio.h>
#include<stdlib.h>
#define L sizeof(struct node)
typedef struct node{
int info;
struct node *next;
}*PN;
int n=0;//指代节点个数
PN Create_List(void)
{
PN head,ptail,pnew;
int val;
head=NULL;
printf("以输入零作为结束:\n");
while (1)
{
if(scanf("%d",&val)==EOF||val<0)
{printf("input error\n");
continue;}
if (val==0)
{
break;
}
pnew=(PN)malloc(L);
pnew->info=val;
pnew->next=NULL;
if (head==NULL)
{
head=pnew;
}else
{
ptail->next=pnew;
}
ptail=pnew;
n++;
}
return head;
}
PN Reverse_Copy(PN head)
{
PN phead,q,r;
phead=NULL;
for (q=head;q!=NULL;q=q->next)
{
r=(PN)malloc(L);
r->info=q->info;
r->next=phead;
phead=r;
}
return phead;
}
void Travel_Print(PN head)
{
PN tail=head;
if (head==NULL)
{
printf("为空链表!") ;
return ;
}
while (tail!=NULL)
{
printf("%d->",tail->info);
tail=tail->next;
}
printf("NULL");
putchar('\n');
}
void Free_List(PN head)
{
PN tail=head;
while (tail!=NULL)
{
tail=tail->next;
free(head);
head=tail;
}
}
int main()
{
PN head,head1;
head=Create_List();
Travel_Print(head);
head1=Reverse_Copy(head);
Travel_Print(head1);
printf("节点个数为:%d\n",n);
//释放内存
Free_List(head);
Free_List(head1);
}
#include<stdlib.h>
#define L sizeof(struct node)
typedef struct node{
int info;
struct node *next;
}*PN;
int n=0;//指代节点个数
PN Create_List(void)
{
PN head,ptail,pnew;
int val;
head=NULL;
printf("以输入零作为结束:\n");
while (1)
{
if(scanf("%d",&val)==EOF||val<0)
{printf("input error\n");
continue;}
if (val==0)
{
break;
}
pnew=(PN)malloc(L);
pnew->info=val;
pnew->next=NULL;
if (head==NULL)
{
head=pnew;
}else
{
ptail->next=pnew;
}
ptail=pnew;
n++;
}
return head;
}
PN Reverse_Copy(PN head)
{
PN phead,q,r;
phead=NULL;
for (q=head;q!=NULL;q=q->next)
{
r=(PN)malloc(L);
r->info=q->info;
r->next=phead;
phead=r;
}
return phead;
}
void Travel_Print(PN head)
{
PN tail=head;
if (head==NULL)
{
printf("为空链表!") ;
return ;
}
while (tail!=NULL)
{
printf("%d->",tail->info);
tail=tail->next;
}
printf("NULL");
putchar('\n');
}
void Free_List(PN head)
{
PN tail=head;
while (tail!=NULL)
{
tail=tail->next;
free(head);
head=tail;
}
}
int main()
{
PN head,head1;
head=Create_List();
Travel_Print(head);
head1=Reverse_Copy(head);
Travel_Print(head1);
printf("节点个数为:%d\n",n);
//释放内存
Free_List(head);
Free_List(head1);
}