初始化线性表模块
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 outputList(SequentialListPtr paraList)
{
for (int i = 0; i < paraList->actualLength; i++) {
printf("%d ", paraList->data[i]);
}
printf("\r\n");
}
void outputMemory(SequentialListPtr paraListPtr)
{
printf("结构体的地址是: %ld\r\n", paraListPtr);
printf("真实长度的地址是: %ld\r\n", ¶ListPtr->actualLength);
printf("数据的地址是: %ld\r\n", ¶ListPtr->data);
printf("真实数据的地址是: %ld\r\n", ¶ListPtr->data[0]);
printf("第二个数据的地址是: %ld\r\n", ¶ListPtr->data[1]);
}
插入函数
void sequentialListInsert(SequentialListPtr paraListPtr, int paraPosition, int paraValue)
{
if (paraListPtr->actualLength >= LIST_MAX_LENGTH)
{
printf("不能插入元素: 表格已满。\r\n");
return;
}
if (paraPosition < 0)
{
printf("不能插入元素: 不支持的类型。");
return;
}
if (paraPosition > paraListPtr->actualLength)
{
printf("不能插入元素: %d 的位置比表的最大位置 %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++;
}
删减函数
int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition)
{
if (paraPosition < 0)
{
printf("非法位置输入: %d.\r\n", paraPosition);
return -1;
}
if (paraPosition >= paraListPtr->actualLength)
{
printf("无法删除该元素: 需要删除位置 %d 的元素比表最后一个元素 %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 sequentialInsertTest()
{
int i;
int tempArray[6] = { 4, 6, 1, 8, 5, 9 };
printf("---- 开始线性表插入测试 ----\r\n");
SequentialListPtr tempList = sequentialListInit(tempArray, 6);
printf("初始化后,线性表为: ");
outputList(tempList);
printf("现在在第一个位置前插入元素8, 插入后线性表为: ");
sequentialListInsert(tempList, 0, 8);
outputList(tempList);
printf("现在在最后一个位置后插入元素9, 插入后线性表为: ");
sequentialListInsert(tempList, 7, 9);
outputList(tempList);
printf("现在插入超过实际长度位置的元素10 \r\n");
sequentialListInsert(tempList, 9, 10);
printf("插入后线性表为:");
outputList(tempList);
for (i = 0; i < 6; i++)
{
printf("插入 %d 中.\r\n", (i + 10));
sequentialListInsert(tempList, 0, (i + 10));
outputList(tempList);
}
printf("---- 线性表插入测试结束 ----\r\n");
}
删减函数测试
void sequentialDeleteTest()
{
int tempArray[6] = { 4, 6, 1, 8, 5, 9 };
printf("---- 开始线性表删除测试 ----\r\n");
SequentialListPtr tempList = sequentialListInit(tempArray, 6);
printf("初始化后, 线性表为: ");
outputList(tempList);
printf("删除第一个位置的元素后, 线性表为: ");
sequentialListDelete(tempList, 0);
outputList(tempList);
printf("删除最后一个位置的元素后, 线性表为: ");
sequentialListDelete(tempList, 5);
outputList(tempList);
printf("删除第二个位置的元素后, 线性表为: ");
sequentialListDelete(tempList, 1);
outputList(tempList);
printf("删除倒数第二个个位置的元素后, 线性表为: ");
sequentialListDelete(tempList, 4);
outputList(tempList);
printf("删除第负六个位置(非法位置)的元素后, 线性表为: ");
sequentialListDelete(tempList, -6);
outputList(tempList);
printf("---- 线性表删除测试结束 ----\r\n");
outputMemory(tempList);
}
辅助函数(定位,寻找,清除)
int getElement(SequentialListPtr paraListPtr, int paraPosition)
{
if (paraPosition < 0)
{
printf("非法位置输入: %d.\r\n", paraPosition);
return -1;
}
if (paraPosition >= paraListPtr->actualLength)
{
printf("无法删除该元素: 位置 %d 的元素比表实际长度 %d 还要大。\r\n", paraPosition, paraListPtr->actualLength);
return -1;
}
return paraListPtr->data[paraPosition];
}
void clearList(SequentialListPtr paraListPtr)
{
paraListPtr->actualLength = 0;
free;
}
定位模块:
可以通过排序函数后利用二分查找完成,之后改完bug补。先浅放个稿子吧。
int comp(const void* a, const void* b)
{
return *(int*)a - *(int*)b;
}
/*int locateElement(SequentialListPtr paraListPtr, int paraValue)
{
qsort(paraListPtr->data, paraListPtr->actualLength, sizeof(int), comp);
for (int i = 0; i < paraListPtr->actualLength; i++)
{
printf("%d ", paraListPtr->data[i]);
}
}*/
运行展示:
完整代码展示:
#include <stdio.h>
#include <malloc.h>
#include<stdlib.h>
#define LIST_MAX_LENGTH 10
int comp(const void* a, const void* b)
{
return *(int*)a - *(int*)b;
}
typedef struct SequentialList
{
int actualLength;
int data[LIST_MAX_LENGTH];
} *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("结构体的地址是: %ld\r\n", paraListPtr);
printf("真实长度的地址是: %ld\r\n", ¶ListPtr->actualLength);
printf("数据的地址是: %ld\r\n", ¶ListPtr->data);
printf("真实数据的地址是: %ld\r\n", ¶ListPtr->data[0]);
printf("第二个数据的地址是: %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("不能插入元素: 表格已满。\r\n");
return;
}
if (paraPosition < 0)
{
printf("不能插入元素: 不支持的类型。");
return;
}
if (paraPosition > paraListPtr->actualLength)
{
printf("不能插入元素: %d 的位置比表的最大位置 %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[6] = { 4, 6, 1, 8, 5, 9 };
printf("---- 开始线性表插入测试 ----\r\n");
SequentialListPtr tempList = sequentialListInit(tempArray, 6);
printf("初始化后,线性表为: ");
outputList(tempList);
printf("现在在第一个位置前插入元素8, 插入后线性表为: ");
sequentialListInsert(tempList, 0, 8);
outputList(tempList);
printf("现在在最后一个位置后插入元素9, 插入后线性表为: ");
sequentialListInsert(tempList, 7, 9);
outputList(tempList);
printf("现在插入超过实际长度位置的元素10 \r\n");
sequentialListInsert(tempList, 9, 10);
printf("插入后线性表为:");
outputList(tempList);
for (i = 0; i < 6; i++)
{
printf("插入 %d 中.\r\n", (i + 10));
sequentialListInsert(tempList, 0, (i + 10));
outputList(tempList);
}
printf("---- 线性表插入测试结束 ----\r\n");
}
int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition)
{
if (paraPosition < 0)
{
printf("非法位置输入: %d.\r\n", paraPosition);
return -1;
}
if (paraPosition >= paraListPtr->actualLength)
{
printf("无法删除该元素: 需要删除位置 %d 的元素比表最后一个元素 %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[6] = { 4, 6, 1, 8, 5, 9 };
printf("---- 开始线性表删除测试 ----\r\n");
SequentialListPtr tempList = sequentialListInit(tempArray, 6);
printf("初始化后, 线性表为: ");
outputList(tempList);
printf("删除第一个位置的元素后, 线性表为: ");
sequentialListDelete(tempList, 0);
outputList(tempList);
printf("删除最后一个位置的元素后, 线性表为: ");
sequentialListDelete(tempList, 5);
outputList(tempList);
printf("删除第二个位置的元素后, 线性表为: ");
sequentialListDelete(tempList, 1);
outputList(tempList);
printf("删除倒数第二个个位置的元素后, 线性表为: ");
sequentialListDelete(tempList, 4);
outputList(tempList);
printf("删除第负六个位置(非法位置)的元素后, 线性表为: ");
sequentialListDelete(tempList, -6);
outputList(tempList);
printf("---- 线性表删除测试结束 ----\r\n");
outputMemory(tempList);
}
int locateElement(SequentialListPtr paraListPtr, int paraValue)
{
for (int i = 0; i < paraListPtr->actualLength; i++)
{
if (paraListPtr->data[i] == paraValue)
{
return i;
}
}
return -1;
}
/*int locateElement(SequentialListPtr paraListPtr, int paraValue)
{
qsort(paraListPtr->data, paraListPtr->actualLength, sizeof(int), comp);
for (int i = 0; i < paraListPtr->actualLength; i++)
{
printf("%d ", paraListPtr->data[i]);
}
}*/
int getElement(SequentialListPtr paraListPtr, int paraPosition)
{
if (paraPosition < 0)
{
printf("非法位置输入: %d.\r\n", paraPosition);
return -1;
}
if (paraPosition >= paraListPtr->actualLength)
{
printf("无法删除该元素: 位置 %d 的元素比表实际长度 %d 还要大。\r\n", paraPosition, paraListPtr->actualLength);
return -1;
}
return paraListPtr->data[paraPosition];
}
void clearList(SequentialListPtr paraListPtr)
{
paraListPtr->actualLength = 0;
free;
}
void main()
{
sequentialInsertTest();
sequentialDeleteTest();
}