构造线性表
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");
}
线性表的赋值
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;
}
//判断索引是否合法
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;
}
//移动索引位置及其后的元素,将位置空出
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("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;
}
查询指定元素第一次出现的索引
int locateElement2(SequentialListPtr list, int value){
for (int i = 0; i < list->actualLength;i++){
//判断元素是否相等
if (list->data[i] == value){
return i;
}
}
//找不到指定元素
return -1;
}
查询指定索引的元素
int getElement2(SequentialListPtr list, int num){
//判断指定索引是否合法
if (num < 0 || num >= list->actualLength){
return -1;}
//得到元素
return list->data[num];
}
清空线性表
void clearList2(SequentialListPtr list){
list->actualLength = 0;
}
获得元素前驱
int priorElment(SequentialListPtr list, int cur_value){
//查找元素索引
int indexCur =locateElement(list, cur_value);
//判断索引是否合法
if(indexCur <= 0){
printf("not found");
return -1;
}
return list->data[indexCur-1];
}
全部代码
//
// 线性表
//
// Created on 2022/4/25.
//
#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){
for(int i = 0;i < paraList->actualLength;i++){
printf("%d ", paraList->data[i]);
}
printf("\r\n");
}
//线性表的赋值
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;
}
//判断索引是否合法
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;
}
//移动索引位置及其后的元素,将位置空出
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);
//在索引为0的位置插入元素
printf("Now insert to the first, the list is: ");
sequentialListInsert(tempList, 0, 8);
//打印
outputList(tempList);
//在索引为6的位置插入元素
printf("Now insert to the last, the list is: ");
sequentialListInsert(tempList, 6, 9);
//打印
outputList(tempList);
//在索引为8的位置插入元素
printf("Now insert beyond the tail. \r\n");
sequentialListInsert(tempList, 8, 9);
printf("The list is:");
//打印Cannot insert element: the position 8 is bigger than the list length 7
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);
//删除索引为0的元素
printf("Now delete the first, the list is: ");
sequentialListDelete(tempList, 0);
outputList(tempList);
//删除索引为3的元素
printf("Now delete the last, the list is: ");
sequentialListDelete(tempList, 3);
outputList(tempList);
//删除索引为1的元素
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);
//删除索引为-6的元素
printf("Now delete the (-6)th, the list is: ");
sequentialListDelete(tempList, -6);
outputList(tempList);
printf("---- sequentialDeleteTest ends. ----\r\n");
outputList(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 locateElement2(SequentialListPtr list, int value){
for (int i = 0; i < list->actualLength;i++){
//判断元素是否相等
if (list->data[i] == value){
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 delete element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
return -1;
}
//得到元素
return paraListPtr->data[paraPosition];
}
//自己的代码
int getElement2(SequentialListPtr list, int num){
//判断指定索引是否合法
if (num < 0 || num >= list->actualLength){
return -1;}
//得到元素
return list->data[num];
}
//清空线性表
//老师的代码
void clearList(SequentialListPtr paraListPtr) {
paraListPtr->actualLength = 0;
}
//自己的代码
void clearList2(SequentialListPtr list){
list->actualLength = 0;
}
//获得元素前驱
int priorElment(SequentialListPtr list, int cur_value){
//查找元素索引
int indexCur =locateElement(list, cur_value);
//判断索引是否合法
if(indexCur <= 0){
printf("not found");
return -1;
}
return list->data[indexCur-1];
}
//main
void main(){
sequentialInsertTest();
sequentialDeleteTest();
}