#include <stdio.h>
#include <malloc.h>
#define LIST_MAX_LENGTH 10
typedef struct SequentialList {
int actualLength;
int data[LIST_MAX_LENGTH]; //数组最大长度可变
} *SequentialListPtr;
void outputList(SequentialListPtr paraList) {
int i;
for(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));
int i;
for (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;
}
// 第二:检查位置
if (paraPosition < 0) {
printf("Cannot insert element: negative position unsupported.");
return;
}
if (paraPosition > paraListPtr->actualLength) {
printf("Cannot insert element: the position %d is bigger than the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
return;
}
// 第三:从最后一个开始往后挪动,腾出位置
int i;
for (i = paraListPtr->actualLength; i > paraPosition; i --) {
paraListPtr->data[i] = paraListPtr->data[i - 1];
}
// 第四:插入
paraListPtr->data[paraPosition] = paraValue;
// 当然第五步我们要记得更新数组长度
paraListPtr->actualLength ++;
}// Of sequentialListInsert
//测试插入函数功能
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);
// 先试试插入第一个位置,即data[0]
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];
int i;
for (i = paraPosition; i < paraListPtr->actualLength; i ++) {
paraListPtr->data[i] = paraListPtr->data[i + 1];
}//Of for i
// 第三. 别忘更新长度
paraListPtr->actualLength --;
// 第四. 返回值.
return resultValue;
}// Of sequentialListDelete
/**
* 测试该函数
*/
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);
}// Of sequentialDeleteTest
/**
* 在顺序表中找到给定的数并返回其位置,没有则返回-1
*/
int locateElement(SequentialListPtr paraListPtr, int paraValue) {
int i;
for (i = 0; i < paraListPtr->actualLength; i ++) {
if (paraListPtr->data[i] == paraValue) {
return i;
}
}
//直接每个一一对比就好了,没找到自然返回-1
return -1;
}
int locateElementTest(){
int i;
int tempArray[5] = {3,7,1,9,6};
//初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
int address = 0;//用于保存返回值
//先测试找不到的情况
printf("测试值:2\n");
address = locateElement(tempList,2);
if(address == -1){
printf("没有找到该值\n");
}
else{
printf("该值的地址是:%d\n",address);
}
//再测试找得到的情况
printf("测试值:7\n");
address = locateElement(tempList,7);
if(address == -1){
printf("没有找到该值\n");
}
else{
printf("该值的地址是:%d\n",address);
}
}
/**
* 找出给定位置的元素的值
*/
int getElement(SequentialListPtr paraListPtr, int paraPosition) {
// 第一步先检查位置 ,将负数和大于数组实际长度的全部截断
if (paraPosition < 0 || paraPosition >= paraListPtr->actualLength) {
printf("不合法的地址: %d.\r\n", paraPosition);
return -1;
}
return paraListPtr->data[paraPosition];
}
void getElementTest(){
int i;
int tempArray[5] = {5,8,7,3,44};
//老样子,先初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
//我们先测试不合法的情况;
getElement(tempList,-1);
getElement(tempList,6);
//然后随机测试两个位置
printf("测试第一个位置:\n");
printf("第%d位置的值是%d\n",1,getElement(tempList,0));
printf("测试第四个位置:\n");
printf("第%d位置的值是%d\n",4,getElement(tempList,3));
}
/**
*清除顺序表中的数据
*/
void clearList(SequentialListPtr paraListPtr) {
paraListPtr->actualLength = 0;
}
void main() {
sequentialInsertTest();
sequentialDeleteTest();
locateElementTest();
getElementTest();
}