#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <malloc.h>
#define LISTLENGTH 10
/*
定义一个线性表结构体变量
*/
typedef struct SequentialList {
int realLength;//实际的表长度
int data[LISTLENGTH];//用数组来存储线性表的数据
} *SequentialListPtr;
/*
遍历输出线性表
*/
void outputList(SequentialListPtr paraList) {
for (int i = 0; i < paraList->realLength; i++)
{
printf("%d", paraList->data[i]);
if (i != paraList->realLength - 1) {
printf(" ");
}
else {
printf("\n");
}
}
}
/*
输出线性表的一些地址
*/
void outputAddress(SequentialListPtr paraListPtr) {
printf("The address of the structure: %ld\r\n", paraListPtr);
printf("The address of actualLength: %ld\r\n", ¶ListPtr->realLength);
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->realLength = paraLength;
return resultPtr;
}
/*
插入数据到线性表
*/
void sequentialListInsert(SequentialListPtr paraListPtr, int paraPosition, int paraValue) {
//检查线性表是否已满
if (paraListPtr->realLength >= LISTLENGTH) {
printf("Cannot insert element: list full.\r\n");
return;
}
//检查传入的位置变量数据是否异常
if (paraPosition < 0) {
printf("Cannot insert element: negative position unsupported.");
return;
}
if (paraPosition > paraListPtr->realLength) {
printf("Cannot insert element: the position %d is bigger than the list length %d.\r\n", paraPosition, paraListPtr->realLength);
return;
}
//把插入位置空出来(后续位置往后挪)
for (int i = paraListPtr->realLength; i > paraPosition; i--) {
paraListPtr->data[i] = paraListPtr->data[i - 1];
}
//插入
paraListPtr->data[paraPosition] = paraValue;
//更新线性表长度
paraListPtr->realLength++;
}
/*
测试插入函数
*/
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->realLength) {
printf("Cannot delete element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->realLength);
return -1;
}
//直接把要删除节点后面的数据往前挪
int resultValue = paraListPtr->data[paraPosition];
for (int i = paraPosition; i < paraListPtr->realLength; i++) {
paraListPtr->data[i] = paraListPtr->data[i + 1];
}
//更新线性表长度
paraListPtr->realLength--;
// 返回删除的数据
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);
// 删除第5个数据(越界)
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");
outputAddress(tempList);
}
/*
查找某一数据的位置
*/
int locateElement(SequentialListPtr paraListPtr, int paraValue) {
for (int i = 0; i < paraListPtr->realLength; 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->realLength) {
printf("Cannot get element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->realLength);
return -1;
}
return paraListPtr->data[paraPosition];
}
/*
清空线性表
*/
void clearList(SequentialListPtr paraListPtr) {
paraListPtr->realLength = 0;
}
void main() {
sequentialInsertTest();
sequentialDeleteTest();
}