-->
当前位置:首页 > 题库 > 正文内容

函数题:顺序表 - 3. 创建线性表

Luz3年前 (2022-03-24)题库1402
定义如下线性表(顺序表)类型。

c
typedef struct
{
int size, length;
LELEMENT *element;
} ALIST;


说明:size 为动态数组的尺寸,length 为线性表的长度,element 为指示动态数组起始地址的指针。

请编写函数,创建线性表(顺序表)。

#### 函数原型

c
void AListCreate(ALIST *list);


说明:参数 list 是指示线性表(顺序表)的指针。函数完成对线性表(顺序表)的初始化:分配动态数组内存空间,记录动态数组尺寸,并将线性表置为空表。

打开“线性表”项目,创建线性表(顺序表)类型的两个文件“AList.h”和“AList.c”。在这两个文件中声明线性表(顺序表)类型和相关的符号常量,并实现线性表的操作函数。

*AList.h*
c
#ifndef _AList_h_
#define _AList_h_

#include "LElement.h"

#define AListInitSize 1024
#define AListIncrement 128

typedef struct
{
int size, length;
LELEMENT *element;
} ALIST;

void AListCreate(ALIST *list);

#endif


说明:AListInitSize 为动态数组初始尺寸;AListIncrement 为动态数组扩容的增量。

*AList.c*
c
#include <stdio.h>
#include <stdlib.h>
#include "AList.h"

/* 你提交的代码将被嵌在这里 */


打开“main.c”,修改主函数对以上函数进行测试。

*main.c*
c
#include <stdio.h>
#include "AList.h"

int main()
{
ALIST a;
AListCreate(&a);
printf("%d %d ", a.size, a.length);
if (a.element != NULL)
{
puts("OK");
}
free(a.element);
return 0;
}


#### 样例输入
in



注:无输入。

#### 样例输出
out
1024 0 OK








答案:若无答案欢迎评论

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。