目录
1.顺序表的静态存储过程
1.初始化
2.销毁
3.清空
4.表长
5.获取数据元素
6.直接前驱
7.直接后继
8.插入
9.删除
10.遍历
11.测试代码
前言
这篇博客主要是顺序表的静态分配存储表示。
1.顺序表的静态存储过程
我们使用静态数据来存储顺序表中的数据元素,静态顺序表定义如下:
//----- 线性表的静态分配顺序存储结构 - - - - -
#define MaxSize 100
typedef int ElementType;
typedef int Status;typedef struct {ElementType data[MaxSize]; // 存储空间基址int length; // 当前长度
}StaticSqList;
1.初始化
表长设置为0.
//初始化
Status initStaticSqList(StaticSqList *sqList){sqList->length = 0;return 1;
}
2.销毁
静态顺序表不需要销毁。
3.清空
// 清空表
void clearSqList(StaticSqList * sqList){sqList->length = 0;
}
4.表长
int staticSqListLength(StaticSqList * sqList){return sqList->length;
}
5.获取数据元素
Status getElementForStaticSqList(StaticSqList *sqList,int pos,ElementType * element){if (pos < 1 || pos > sqList->length) {return 0;}*element = sqList->data[pos-1];return 1;
}
数据类型(data type)是一个值的集合和定义在这个集合上的一组操作的总称。
6.直接前驱
// 获取直接前驱 获取element的前一个数据元素priorElement
Status priorStaticSqListElement(StaticSqList * sqList,ElementType element,ElementType * priorElement){for (int i = 0; i<sqList->length; i++) {if (sqList->data[i] == element) {//找到数据元素if (i>=1) {* priorElement = sqList->data[i-1];return 1;}}}return 0;
}
7.直接后继
// 获取直接后继 获取element的后一个数据元素nextElement
Status nextStaticSqListElement(StaticSqList * sqList,ElementType element,ElementType * nextElement){for (int i = 0; i<sqList->length; i++) {if (sqList->data[i] == element) {//找到数据元素if (i+1 <= sqList->length-1) {* nextElement = sqList->data[i + 1];return 1;}}}return 0;
}
8.插入
Status insertStaticSqList(StaticSqList * sqList,int pos,ElementType element){if (pos < 1 || pos > sqList->length + 1 || sqList->length == MaxSize) {return 0;}for (int i = sqList->length; i > pos - 1; i--) {sqList->data[i] = sqList->data[i-1];}sqList->data[pos -1] = element;sqList->length++;return 1;
}
9.删除
Status deleteSqList(StaticSqList *sqList,int pos,ElementType * element){if (pos < 1 || pos > sqList->length) {return 0;}for (int i = pos - 1; i < sqList->length - 1; i++) {sqList->data[i] = sqList->data[i+1];}* element = sqList->data[pos -1];sqList->length--;return 1;
}
10.遍历
// 遍历顺序表
void travseStaticSqList(StaticSqList *sqList){for (int i = 0; i<sqList->length; i++) {printf("%d\t",sqList->data[i]);}printf("\n");
}
11.测试代码
//初始化
Status initStaticSqList(StaticSqList *sqList){sqList->length = 0;return 1;
}
// 清空表
void clearSqList(StaticSqList * sqList){sqList->length = 0;
}
// 判断链表是否为空
Status staticSqListEmpty(StaticSqList *sqList){return sqList->length == 0;
}
int staticSqListLength(StaticSqList * sqList){return sqList->length;
}
Status getElementForStaticSqList(StaticSqList *sqList,int pos,ElementType * element){if (pos < 1 || pos > sqList->length) {return 0;}*element = sqList->data[pos-1];return 1;
}
// 获取直接前驱 获取element的前一个数据元素priorElement
Status priorStaticSqListElement(StaticSqList * sqList,ElementType element,ElementType * priorElement){for (int i = 0; i<sqList->length; i++) {if (sqList->data[i] == element) {//找到数据元素if (i>=1) {* priorElement = sqList->data[i-1];return 1;}}}return 0;
}
// 获取直接后继 获取element的后一个数据元素nextElement
Status nextStaticSqListElement(StaticSqList * sqList,ElementType element,ElementType * nextElement){for (int i = 0; i<sqList->length; i++) {if (sqList->data[i] == element) {//找到数据元素if (i+1 <= sqList->length-1) {* nextElement = sqList->data[i + 1];return 1;}}}return 0;
}
Status insertStaticSqList(StaticSqList * sqList,int pos,ElementType element){if (pos < 1 || pos > sqList->length + 1 || sqList->length == MaxSize) {return 0;}for (int i = sqList->length; i > pos - 1; i--) {sqList->data[i] = sqList->data[i-1];}sqList->data[pos -1] = element;sqList->length++;return 1;
}
Status deleteSqList(StaticSqList *sqList,int pos,ElementType * element){if (pos < 1 || pos > sqList->length) {return 0;}for (int i = pos - 1; i < sqList->length - 1; i++) {sqList->data[i] = sqList->data[i+1];}* element = sqList->data[pos -1];sqList->length--;return 1;
}
Status appendElement(StaticSqList * sqList,ElementType element){if (sqList->length == MaxSize) {//数组已满return 0;}sqList->data[sqList->length++] = element;return 1;
}
// 遍历顺序表
void travseStaticSqList(StaticSqList *sqList){for (int i = 0; i<sqList->length; i++) {printf("%d\t",sqList->data[i]);}printf("\n");
}
void commonPrint(const char * string){printf("**********\t%s测试\t**********\n",string);
}
//测试顺序表的方法
void testStaticSqList(void){StaticSqList sqList;commonPrint("顺序表的静态存储表示");if (initStaticSqList(&sqList)) {printf("顺序表初始化成功\n");}commonPrint("判断顺序表的是否为空");if (staticSqListEmpty(&sqList)) {printf("顺序表为空,顺序的长度:%d\n",staticSqListLength(&sqList));}commonPrint("顺序表中的插入测试");for (int i = 1; i<= 10; i++) {if (appendElement(&sqList, i)) {
// printf("数据元素%d初始化成功\n",i+1);} else {
// printf("插入失败\n");}}printf("插入之前的数组:\n");travseStaticSqList(&sqList);printf("顺序的长度:%d\n",staticSqListLength(&sqList));if (insertStaticSqList(&sqList,5,11)) {//非法位置printf("数据元素插入成功\n");} else {printf("插入失败\n");}printf("插入之后的数组:\n");travseStaticSqList(&sqList);printf("顺序的长度:%d\n",staticSqListLength(&sqList));commonPrint("顺序表中的删除测试");int deleteElement;if (deleteSqList(&sqList,11,&deleteElement)) {//删除printf("数据元素%d删除成功\n",deleteElement);} else {printf("删除失败\n");}printf("删除之后的顺序表:\n");travseStaticSqList(&sqList);commonPrint("获取顺序表中的数据元素");for (int i = 0; i<= 11; i++) {ElementType element;if (getElementForStaticSqList(&sqList, i, &element)) {printf("数据元素获取成功,第%d个数据元素为:%d\n",i,element);}else{printf("第%d个数据元素获取失败\n",i);}}commonPrint("获取顺序表中某个数据元素的直接前驱");ElementType priorElem;if (priorStaticSqListElement(&sqList, 2, &priorElem)) {printf("数据元素直接前驱获取成功,前驱数据元素为:%d\n",priorElem);}else{printf("数据元素直接前驱获取失败\n");}commonPrint("获取顺序表中某个数据元素的直接后继");ElementType nextElem;if (nextStaticSqListElement(&sqList, 8, &nextElem)) {printf("数据元素直接后继获取成功,后继数据元素为:%d\n",nextElem);}else{printf("数据元素直接后继获取失败\n");}commonPrint("顺序表中的数据元素测试");}