急!用c语言实现链表
就是链表的创建和输出,我写的为什么运行不了啊??
先是建立个空表头,然后依次插入,构成链表,最后是输出;
谢谢~
以下是我写的程序……
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct list)
struct list
{
int num;
long data;
struct list *next;
};
struct list*head;
struct list initlist (struct list * head)
{
head=(struct list *)malloc(LEN);
head->next=NULL;
}
struct list creat (struct list *head,int n)
{
struct list *p;
int i;
initlist(head);
for(i=n;i>0;--i)
{
p=(struct list *)malloc(LEN);
scanf("%d,%f",&p->num,&p->data);
p->next=head->next;
head->next=p;
}
}
int print(struct list*head)
{
struct list *p1;
p1=head;
if (head!=0)
{
printf("%d,%f",p1->num,p1->data);
p1=p1->next;
}while(p1!=0);
}
main()
{
initlist(head);
creat(head,3);
print(head);
getchar();
getchar();
}