古老的C语言来实现动态数组
本章的逻辑参照 java版本的<编写一个属于自己的ArrayList>
编译器Xcode ,当然你也可以用其它编译器,自己选择
开饭啦,开饭啦,开饭啦,开饭啦
闲话不多说,看代码:
项目结构
详细代码
list.h文件
//// list.h// chapter01-顺序存储结构//// Created by Mac on 2020/5/15.// Copyright © 2020 Mac. All rights reserved.//typedef int ElemType; //元素类型typedef int Status; /*函数返回值状态*/typedef struct{ElemType *elements;/*创建一个动态数组*/int length; /*线性表的长度*/}SqList;Status checkRange(int index,const SqList* L); //检查是否合法Status checkOfAddRange(int index,const SqList* L);/*初始化顺序表*/SqList initSqList();/*在指定位置插入数据*/Status listInsert(int index,ElemType element,SqList* L);//在指定位置插入/*在最后位置插入数据*/Status listLastInsert(ElemType element,SqList *L);//在最后位置插入/*删除指定位置的数据*/ElemType listDelete(int index,SqList* L); //删除指定位置的元素,并返回指定元素/*清除线性表*/Status clearList(SqList *L);/*获取指定位置的元素*/ElemType getElement(int index, const SqList *L);/*查看元素的位置*/int indexOf(ElemType e,const SqList* L);/*获取线性表的长度*/int length(SqList* L);/*判断是否包含某个元素*/bool contains(ElemType e,const SqList *L);/* 设置index对应的元素,并返回原来的元素值*/ElemType set(ElemType e,int index,SqList *L);/*判断是否为空*/bool isEmpty(const SqList *L);
list.c文件
//// list.c// chapter01-顺序存储结构//// Created by Mac on 2020/5/15.// Copyright © 2020 Mac. All rights reserved.///*顺序存储线性表的初始化*/SqList initSqList(){SqList list;list.elements = malloc(sizeof(ElemType)*DEFAULT_SIZE);//动态的申请空间,并将空间初始化为0if (!list.elements) {printf("初始化失败");exit(0);}list.length = 0;return list;}/*添加操作*/Status listInsert(int index,ElemType element,SqList* L){//检查异常if(!checkOfAddRange(index, L)) return ERROR;//考虑是否需要扩容for (int i = L->length; i > index; i--) {L->elements[i] = L->elements[i-1];}L->elements[index] = element;L->length++;return OK;}/* 删除操作 */ElemType listDelete(int index,SqList* L){if (!checkRange(index, L)) return ERROR;//考虑是否需要缩容ElemType data = L->elements[index];for (int i = index+1; i < L->length; i++) {L->elements[i-1] = L->elements[i];}L->length --;return data;}/* 插入在最后的位置 */Status listLastInsert(ElemType element,SqList *L){return listInsert(L->length, element, L);}/*清空操作*/Status clearList(SqList *L){L->length = 0;return OK;}/*获取指定下标的值*/ElemType getElement(int index, const SqList* L){checkRange(index, L);return L->elements[index];}/*判断线性表是否存在某个元素*/int indexOf(ElemType e,const SqList* L){for (int i = 0; i < L-> length; i++) {if (L->elements[i] == e)return i;}return NOT_FOUND_ELEMENT;}/*获取线性表的长度*/int length(SqList* L){return L->length;}bool contains(ElemType e,const SqList *L){return (indexOf(e, L) != NOT_FOUND_ELEMENT);}ElemType set(ElemType e,int index,SqList *L){checkRange(index,L);ElemType oldelement = L->elements[index];L->elements[index] = e;return oldelement;}/*遍历所有元素*/void disPlaySqList(const SqList* L){printf("size = %d [",L->length);for (int i = 0; i< L->length; i++) {printf("%d ",L->elements[i]);}printf("] \n");}bool isEmpty(const SqList *L){return L->length == 0;}Status checkRange(int index,const SqList* L){if(index < 0 || index >= L->length){printf("===异常啦===\n");exit(0);return ERROR;}return OK;}Status checkOfAddRange(int index,const SqList* L){/*添加的时候*/if(index < 0 || index > L->length){printf("===异常啦===\n");exit(0);return ERROR;}return OK;}
main.c
//// main.c// chapter01-顺序存储结构//// Created by Mac on 2020/5/15.// Copyright © 2020 Mac. All rights reserved.//int main(int argc, const char * argv[]) {SqList list = initSqList();listLastInsert(21, &list);listInsert(0, 11, &list);listInsert(0, 31, &list);printf("%d \n",getElement(1, &list));listDelete(2, &list);printf("是否包含:%d \n",contains(21, &list));printf("线性表的长度:%d \n",length(&list));set(99, 0, &list);disPlaySqList(&list);clearList(&list);disPlaySqList(&list);}
有啥好的建议,和代码有啥错误,望指正~
