简单的单链表问题,谢谢啦
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *creatlist(int a[])
{
struct node *h,*p,*q;
int i;
h=NULL;
for(i=0;i<6;i++)
{
q=(struct node*)malloc(sizeof(struct node));
q->data=a[i];
q->next=NULL;
if(h==NULL) h=p=q;
else
{
p->next=q;
p=q;
}
}
return h;
}
void outlist(struct node *h)
{
struct node *p;
p=h;
if(p==NULL)
printf("The list is NULL\n");
else
{
printf("head");
do
{
printf("->%d",p->data);
p=p->next;
}
while(p!=NULL);
printf("->end\n");
}
}
void main()
{
struct node *head;
int a[6]={2,4,10,7,5,9};
head=creatlist(a);
printf("\nThe original list :\n");
outlist(head);
}
我想问一下,在函数struct node *creatlist(int a[])中,为什么要把h赋值NULL,
还有就是在函数void outlist(struct node *h),为什么要写
if(p==NULL)
printf("The list is NULL\n");
链表不是已经生成了吗,写这个有什么作用,谢谢了