代码有点疑问?
#include "stdio.h"#include "string.h"
#include "malloc.h"
#define NULL 0
#define LEN sizeof(struct stu)
struct stu
{
int num;
struct stu *next;
}*p1,*p2;
struct stu *creat()
{
int temp;
struct stu *head=NULL;
printf("input temp:\n");
scanf("%d",&temp);
while(temp!=0)
{
p1=(struct stu *)malloc(LEN);
if(head==NULL)
head=p1;
else
p2->next=p1;
p1->num=temp;
p2=p1;
printf("\ninput number:");
scanf("%d",&temp);
}
p2->next=NULL;
return(head);
}
void output(struct stu *head)
{
for(p1=head;p1!=NULL;p1=p1->next)
{
printf("%4d",p1->num);
p1=p1->next;
}
}
struct stu *turnback(struct stu *head)
{
struct stu *a,*newhead,*temp;
p2=head;
p1=head->next;
head->next=NULL;
while(p1!=NULL) //这个while循环是如何将链表逆顺序输出的?
{
temp=p1->next;//这个p1->next是指第一个结点还是第二个节点?
p1->next=p2;
p2=p1;
p1=temp;
}
newhead=p2;
return(newhead);
}
int main(int argc, char* argv[])
{
struct stu *head;
head=creat();
printf("the original list:\n");
output(head);
head=turnback(head);
printf("\nthe new list:\n");
output(head);
printf("\n");
return 0;
}