6-2 单链表 - 2. 创建线性表 (10 分)
定义如下线性表(单链表)类型。
typedef struct
{
int length;
LNODE *head;
} LLIST;
说明:线性表采用带附加头结点的单链表存储结构。length 为线性表的长度,head 为头指针。
请编写函数,创建线性表(单链表)。
函数原型
void LListCreate(LLIST *list);
说明:参数 list 是指示线性表(单链表)的指针。函数完成对线性表(单链表)的初始化:创建附加头结点,将线性表初始长度置为0。
打开“线性表”项目,创建线性表(单链表)类型的两个文件“LList.h”和“LList.c”。
在“LList.h”头文件中声明线性表(单链表)类型和相关的操作函数,在“LList.c”源程序文件中编写相关操作函数。修改“main.c”源程序文件中的主函数,对以上函数进行测试。
LList.h
#ifndef LList_h
#define LList_h
#include "LNode.h"
typedef struct
{
int length;
LNODE *head;
} LLIST;
void LListCreate(LLIST *list);
#endif
LList.c
#include <stdio.h>
#include <stdlib.h>
#include "LList.h"
/* 你提交的代码将被嵌在这里 */
main.c
#include <stdio.h>
#include "LList.h"
int main()
{
LLIST a;
LListCreate(&a);
printf("%d\n", a.length);
if (a.head != NULL && a.head->next == NULL)
{
puts("OK");
}
free(a.head);
return 0;
}
样例输入
注:无输入。
样例输出
0
OK