改代码实现以下几个功能:
1.输出表中所有元素
2.输出结构体中各种元素的地址
3.初始化顺序表
4.在下标为paraposition的地方插入paravalue
5.测试插入函数
6.删除下标为paraposition的元素,并返回其值
7.测试删除函数
8.通过元素返回元素下标,元素不存在,返回-1
9.通过下标返回对应元素
10.将顺序表清零
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <malloc.h>
#define LIST_MAX_LENGTH 10
/*记录顺序表的元素个数以及元素
* **/
typedef struct SequentialList {
int actualLength;
int data[LIST_MAX_LENGTH]; //The maximum length is fixed.
} *SequentialListPtr;
/**
* 输出顺序表中的所有元素
*/
void outputList(SequentialListPtr paraList) {
for (int i = 0; i < paraList->actualLength; i++) {
printf("%d ", paraList->data[i]);
}
printf("\r\n");
}
/**
* 输出地址
*/
void outputMemory(SequentialListPtr paraListPtr) {
printf("The address of the structure: %ld\r\n", paraListPtr);
printf("The address of actualLength: %ld\r\n", ¶ListPtr->actualLength);
printf("The address of data: %ld\r\n", ¶ListPtr->data);
printf("The address of actual data: %ld\r\n", ¶ListPtr->data[0]);
printf("The address of second data: %ld\r\n", ¶ListPtr->data[1]);
}
/*初始化一个结构并返回一个结构体指针,将一个数组中的值传入到结构体中的数组*/
SequentialListPtr sequentialListInit(int paraData[], int paraLength) {
SequentialListPtr resultPtr = (SequentialListPtr)malloc(sizeof(struct SequentialList));
for (int i = 0; i < paraLength; i++) {
resultPtr->data[i] = paraData[i];
}
resultPtr->actualLength = paraLength;
return resultPtr;
}
void sequentialListInsert(SequentialListPtr paraListPtr, int paraPosition, int paraValue) {
// 确认是否有足够的空间
if (paraListPtr->actualLength >= LIST_MAX_LENGTH) {
printf("Cannot insert element: list full.\r\n");
return;
}
//小于0就不能插入数字
if (paraPosition < 0) {
printf("Cannot insert element: negative position unsupported.");
return;
}
//大于length会越界
if (paraPosition > paraListPtr->actualLength) {
printf("Cannot insert element: the position %d is bigger than the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
return;
}
// 元素向后移动一个位置
for (int i = paraListPtr->actualLength; i > paraPosition; i--) {
paraListPtr->data[i] = paraListPtr->data[i - 1];
}
// 插入新元素
paraListPtr->data[paraPosition] = paraValue;
// 更新元素个数
paraListPtr->actualLength++;
}
void sequentialInsertTest() {
int i;
int tempArray[5] = { 3, 5, 2, 7, 4 };
printf("---- sequentialInsertTest begins. ----\r\n");
//初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 插入到第一个
printf("Now insert to the first, the list is: ");
sequentialListInsert(tempList, 0, 8);
outputList(tempList);
// 在尾部插入
printf("Now insert to the last, the list is: ");
sequentialListInsert(tempList, 6, 9);
outputList(tempList);
printf("Now insert beyond the tail. \r\n");
sequentialListInsert(tempList, 8, 9);
printf("The list is:");
outputList(tempList);
// 插入三个元素
for (i = 0; i < 5; i++) {
printf("Inserting %d.\r\n", (i + 10));
sequentialListInsert(tempList, 0, (i + 10));
outputList(tempList);
}
printf("---- sequentialInsertTest ends. ----\r\n");
}
int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition) {
if (paraPosition < 0) {
printf("Invalid position: %d.\r\n", paraPosition);
return -1;
}
if (paraPosition >= paraListPtr->actualLength) {
printf("Cannot delete element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
return -1;
}
int resultValue = paraListPtr->data[paraPosition];
//覆盖掉要删除的值,把该元素后面的值向前移动一位
for (int i = paraPosition; i < paraListPtr->actualLength; i++) {
paraListPtr->data[i] = paraListPtr->data[i + 1];
}
//更新长度
paraListPtr->actualLength--;
// 返回删除的值
return resultValue;
}
void sequentialDeleteTest() {
int tempArray[5] = { 3, 5, 2, 7, 4 };
printf("---- sequentialDeleteTest begins. ----\r\n");
// 初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 删除首个
printf("Now delete the first, the list is: ");
sequentialListDelete(tempList, 0);
outputList(tempList);
// 删除最后一个
printf("Now delete the last, the list is: ");
sequentialListDelete(tempList, 3);
outputList(tempList);
// 删除第二个
printf("Now delete the second, the list is: ");
sequentialListDelete(tempList, 1);
outputList(tempList);
//删除第六个
printf("Now delete the 5th, the list is: ");
sequentialListDelete(tempList, 5);
outputList(tempList);
printf("Now delete the (-6)th, the list is: ");
sequentialListDelete(tempList, -6);
outputList(tempList);
printf("---- sequentialDeleteTest ends. ----\r\n");
outputMemory(tempList);
}
/**
返回元素下标,如果元素不存在,则返回-1
*/
int locateElement(SequentialListPtr paraListPtr, int paraValue) {
for (int i = 0; i < paraListPtr->actualLength; i++) {
if (paraListPtr->data[i] == paraValue) {
return i;
}
}
return -1;
}
/**
通过下标索引元素
*/
int getElement(SequentialListPtr paraListPtr, int paraPosition) {
if (paraPosition < 0) {
printf("Invalid position: %d.\r\n", paraPosition);
return -1;
}
if (paraPosition >= paraListPtr->actualLength) {
printf("Cannot get element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
return -1;
}
return paraListPtr->data[paraPosition];
}
/**
将所有元素清零
*/
void clearList(SequentialListPtr paraListPtr) {
paraListPtr->actualLength = 0;
}
void main() {
sequentialInsertTest();
sequentialDeleteTest();
}
插入函数测试过程如下:
删除函数测试过程如下:
整个代码运行结果如下(vs实现):
总结:
这段代码实现了一个固定大小的顺序表(Sequential List)的基本操作,包括初始化、插入、删除、查找、获取元素、清空列表以及输出列表和内存地址信息。顺序表的最大长度通过宏LIST_MAX_LENGTH定义为10。顺序表使用结构体SequentialList表示,其中包含一个整型数组data用于存储元素,和一个整型变量actualLength用于记录当前顺序表的实际长度。总的来说,这段代码是顺序表数据结构的一个简单实现,展示了顺序表的基本操作和使用场景。通过固定大小的数组来存储数据,使得数据的访问速度快,但同时也限制了顺序表的最大容量。