纯练习,试了一些输入,结果是对的,供楼主参考
#include <stdio.h>
#include <stdlib.h>
typedef struct testlist
{
int value;
struct testlist *next;
}testlist,*p1,*p2;
testlist initlist(testlist **listhead)
{
testlist *p1,*p2;
*listhead=p1=p2=(testlist *)malloc(sizeof(testlist));
scanf("%d",&p1->value);
p1=(testlist *)malloc(sizeof(testlist));
scanf("%d",&p1->value);
while(p1->value != 0)
{
p2->next=p1;
p2=p1;
p2->next=NULL;
p1=(testlist *)malloc(sizeof(testlist));
scanf("%d",&p1->value);
}
}
testlist converse(testlist **listhead)
{
testlist *p0,*p1,*p2;
p0=p1=*listhead;
while(p1->next != NULL)
p1=p1->next;
*listhead=p2=p1;
while(p2->next != p0)
{
p1=p0;
while(p1->next != p2)
p1=p1->next;
p2->next=p1;
p2=p1;
}
p0->next=NULL;
}
void outputlist(testlist *listhead)
{
testlist *p1;
p1=listhead;
while(p1->next != NULL)
{
printf("%d
",p1->value);
p1=p1->next;
}
printf("%d\n",p1->value);
}
int main(void)
{
testlist *head=NULL;
initlist(&head);
outputlist(head);
converse(&head);
outputlist(head);
}