结构应用:逆序链表何错之有?
运行到后来好像进入死循环啦,错在哪里啊?
哪位大虾指点一下。给个正确的程序,指出错误的地方。
--
#include <stdio.h>
#include <stdlib.h>
struct stu
{
int num;
struct stu *next;
}*p1,*p2;
struct stu *creat(void )
{
int temp;
struct stu *head=NULL;
printf("\ninput number:");
while(scanf("%d",&temp))
{
p1=(struct stu *)malloc(sizeof(struct stu));
if(head==NULL)
head=p1;
else
p2->next=p1;
p1->num=temp;
printf("Input number:");
p2=p1;
}
p2->next=NULL;
return head;
}
void output(struct stu *outhead)
{
for(p1=outhead;p1!=NULL;printf("%4d",p1->num),p1=p1->next);
}
struct stu *turnback(struct stu *head)
{
struct stu *myNew,* myNewhead=NULL;
do
{
p2=NULL;
p1=head;
while(p1->next!=NULL)p2=p1,p1=p1->next;
if(myNewhead==NULL)myNewhead=p1,myNew=myNewhead->next=p2;
myNew=myNew->next=p2;
}while(head->next!=NULL);
return myNewhead;
}
void main()
{
struct stu *head;
head=creat();
printf("\n\nthe original list:");
output(head);
head=turnback(head);
printf("\n\nThe myNew list:");
output(head);
}
--