C链表的逆序的问题??
#include<stdio.h>#include<malloc.h>
#define NULL 0
struct student
{
int num;
char name[20];
struct student *next;
}stu;
struct student *creat(struct student *head)
{
struct student *p1, *p2;
int n=0;
p1=(struct student *)malloc(sizeof(struct student));
printf("please input the data:\n");
while((++n)<=5) //依次输入学生数据;
{
scanf("%d,%s",&p1->num, p1->name);
if(n==1)
{
head=p1;
}
p2=p1;
p1=(struct student *)malloc(sizeof(struct student)); //开辟新结点;
p2->next=p1;
}
p2->next=NULL; //结束链表;
return(head);
}
struct student *nixu(struct student *head) //逆序链表函数; 我的想法是将原来链表的从尾到头赋给以个新的链表,但不知哪里错了。
{
int i;
struct student *p1, *p2, *new_head, *p, *pnew;
p1=p2=head;
p=pnew=new_head=(struct student *)malloc(sizeof(struct student));
for(i=0;i<5;i++)
{
p1=head;
while(1)
{
p2=p1;
p1=p1->next;
if(p1->next==NULL)
{
break;
}
}
p2->next=NULL;
if(i==0)
{
new_head=p1;
}
pnew=p=p1;
p=(struct student *)malloc(sizeof(struct student));
pnew->next=p;
}
pnew->next=NULL;
return(new_head);
}
void main()
{
struct student *head, *p=NULL;
head=&stu;
head=creat(head);
p=nixu(head);
printf("please output the lianbiao:\n");
while(1)
{
printf("%d, %s\n", p->num, p->name);
if(p->next=NULL)
{
break;
}
p=p->next;
}
}