#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef struct Book
{
char id[20];
char name[30];
float price;
}Book;
typedef struct List
{
Book book;
struct List *next;
}List,*LinkList;
//初始化链表
Status InitList(LinkList *L)
{
*L = (LinkList)malloc(sizeof(Book));
if(!(*L))
return ERROR;
(*L)->next = NULL;
return OK;
}
//将L重置为空表
Status ClearList(LinkList *L)
{
LinkList p,q;
if(!(*L))
return 0;
p=(*L)->next; /* p指向第一个结点 */
while(p) /* 没到表尾 */
{
q=p->next;
//printf("ff");
free(p);
p=q;
}
(*L)->next=NULL; /* 头结点指针域为空 */
return OK;
}
void AddNumber(LinkList p)
{
strcpy(p->book.id,"1");
strcpy(p->book.name,"平凡的世界");
p->book.price=rand()%100+10;
}
void CreateListTail(LinkList *L,int n)
{
LinkList p,r;
(*L)=(LinkList)malloc(sizeof(Book));
r=*L;
while(n--){
p=(LinkList)malloc(sizeof(Book));
AddNumber(p);
r->next=p;
r=p;
}
r->next=NULL;
}
int main()
{
LinkList L;
InitList(&L);
CreateListTail(&L,3);
if(ClearList(&L))
{
printf("数据已经清空!\n");
}
}
出现了这样的情况!
求大神解释