这里是如何交换值的?
#include "stdafx.h"#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);
}
}
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;
}