我的代码参照了严蔚敏、吴伟民编写的数据结构(C语言版)
所有代码采用C语言编写。讲解请查看注释。
*另注:书中很多函数采用了引用传值,如InitList(Sqlist &L)。但C语言并不支持引用传值,因此统一改为地址传值,即InitList(Sqlist *L)。
头文件及宏定义
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//线性表动态分配存储结构
#define LIST_INIT_SIZE 100//初始分配量
#define LISTINCREMENT 10//分配增量
#define OK 1
#define Fail 0
#define False 0
#define True 1
typedef定义数据类型和结构体
typedef int ElemType;
typedef int Status;
typedef struct{
ElemType* elem;
int length;//长度
int listsize;//存储容量 //长度<=存储容量
}Sqlist;
InitList(创建)
Status InitList_Sq(Sqlist* L){
//创建
//地址方式调用 ,获得传入参数(地址)对应的对象
L->elem=(ElemType*)malloc(LIST_INIT_SIZE* sizeof(ElemType));//分配内存空间 ,malloc的返回值是一个指针,指向一段可用内存的起始地址
if(!L->elem)exit(OVERFLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
DestroyList(销毁)
Status DestroyList_Sq(Sqlist