如何交换值?
#include "stdio.h"#include "malloc.h"
#define LEN sizeof(struct stu)
struct stu
{
int num;
struct stu *next;
}*p1,*p2;
struct stu *creat()
{
int temp;
struct stu *head=NULL;
printf("\ninput number:");
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;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=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;
}