单链表哪里出错了
#include <stdio.h>#include <stdlib.h>
#define ERROR 0
#define OK 1
//typedef int ElemType;
#define ElemType int
typedef struct Node
{
ElemType data; //Êý¾ÝÓò
struct Node *next; //Ö¸ÕëÓò
}Node ,*LinkList;
void CreateListHead(LinkList *L, int n)
{
LinkList p,r;
int i=1;
srand(time(0)); //初始化随机种子
*L = (LinkList)malloc(sizeof(struct Node)); //先建立一个带头结点的单链表
if(*L == NULL)
{
fprintf(stderr, "malloc() error.\n");
return ERROR;
}
(*L)->next = NULL;
while(i <= n)
{
p = (LinkList)malloc(sizeof(struct Node));
if( p == NULL)
{
fprintf(stderr, "malloc() error.\n");
return ERROR;
}
p->data = rand() % 100 + 1;
p->next = (*L)->next;
(*L)->next = p; //插入到表头
i++;
}
r=*L; //尾部结点
for(i=0; i<n; i++)
{
p=(Node *) malloc(sizeof(Node));
p->data=rand()%100+1;
r->next =p;
r=p;
}
}
int ClearList(LinkList *L)
{
LinkList p,q;
p=(*L)->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
(*L)->next=NULL;
return OK;
}
int printList(LinkList L)
{
LinkList p;
p = L->next;
if(p == NULL)
{
printf("链表为空.\n");
return ERROR;
}
while(p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return OK;
}
int main()
{
LinkList L;
printf("Hello world!\n");
CreateListHead(&L,1);
printList(L);
return 0;
}