初次写链表程序,求帮忙检查错误
新建一个链表,然后将链表逆序输出,编译连接无错,但是无法得到正确结果#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct data)
struct data
{
int num;
struct data *next;
};
struct data * creat(void);
struct data * sort(struct data *);
void print(struct data *);
void main()
{
struct data * head;
printf("请输入数据(输入结束请按0):\n");
head=creat();
printf("您输入的数据为:\n");
print(head);
head=sort(head);
printf("逆序后的数据为:\n");
print(head);
}
struct data * creat(void)
{
int n=0;
struct data * Head,*p1,*p2;
p1=p2=(struct data *)malloc(LEN);
scanf("%d",&p1->num);
Head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
Head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct data *)malloc(LEN);
scanf("%d",&p1->num);
}
p2->next=NULL;
return Head;
}
struct data * sort(struct data * Head)
{
struct data *p,*NewHead=NULL;
p=NewHead=Head;
while(NewHead->next!=NULL)
{
NewHead=NewHead->next;
NewHead->next=p;
p=NewHead;
}
Head->next=NULL;
return NewHead;
}
void print(struct data * Head)
{
struct data *p;
p=Head;
if(Head!=NULL)
do
{
printf("%6d",p->num);
p=p->next;
}while(p!=NULL);
printf("\n");
}