链表倒置,求解答 为什么打印出一个0呢。。还有 第二个打印打不出来啊。。。
#include<stdio.h>#include<stdlib.h>
#include<malloc.h>
typedef struct Lnode{
int data;
struct Lnode *next;
}*LIST;
LIST CreateList(int n)
{
int a,i;
LIST p,q,head;
head=p=(LIST)malloc(sizeof(LIST));
p->data = 0;
for(i=0;i<n;i++)
{
q=(LIST) malloc (sizeof(LIST));
printf("please input the data:\n");
scanf("%d",&a);
q->data = a;
p->next = q;
}
return head;
}
void PrintList(LIST head)
{
LIST p;
printf("链表当前数据:\n");
p=head;
while(p->next!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
}
LIST ReverseList(LIST head)
{
LIST q,p,n;
if(head=NULL)
return NULL;
p=head->next;
q=p->next;
while(q)
{
n=q->next;
q->next=p;
p=q;
q=n;
}
head->next->next=NULL;
}
void main()
{
LIST head,afhead;
int n;
printf("please input the nunber of your data:");
scanf("%d",&n);
head=CreateList(n);
PrintList(head);
afhead=ReverseList(head);
PrintList(afhead);
}