1、代码实现
#include <stdio.h>
# include<malloc.h>
#define LIST_MAX_LENGTH 10;
/**
* 线性表,关键是数据
* */
typedef struct SequentialList{
int actualLength ;
int data[LIST_MAX_LENGTH] ;
} *SequentialListPtr; //定义顺序表
void ouputList(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" , pararListPtr);
printf("The address of the actualLength : %ld \r\n" , ¶ListPtr -> actualLength);
printf("The address of the data : %ld \r\n" , ¶ListPtr -> data);
printf("The address of the actual data: %ld \r\n" , ¶ListPtr -> data[0]);
printf("The address of the second data : %ld \r\n" , ¶ListPtr -> data[1]);
}
/**
* 初始化顺序表
* 传入数组,将其元素初始化结构体
* */
SequentialListPtr SequentialListInit(int paraData[] , int paraLength){
SequentialListPtr resultPtr = (SequentialListPtr)malloc(sizeof(struct Sequential));
// 开辟一个Sequential 结构体内存空间,把地址赋给 resultPtr
for(int i = 0 ; i < paraLength ; i++){
resultPre -> data[i] = paraData[i];
}
resultPtr -> actualLenth = paraLength;
return resultPtr;
}
/**
* 顺序表插入元素
* paraList 是顺序表的地址
* paraPosition 是插入位置
* paraValue 是插入元素
* */
void sequentialListInsert(SequentialListPtr paraListPtr , int paraPosition ,int paraValue){
// 先检查顺序表的实际长度,保证其在 1 到 10
if(paraListPtr -> actualLength >= LIST_MAX_LENGTH){
printf("Cannot insert element : list full. \r\n");
return ;
}
if(paraPosition < 0){
printf("Cannot insrt element : negative position unsupported.");
return;
}
if(paraPosition > paraListPtr -> actualLength){
printf("Cannot insrt element : the positiong %d is bigger than the list lenth");
return ;
}
// 将 position 插入位置之后的元素向后移动,将value 赋给 position 对应的元素
for(int i = paraListPtr -> actualLength ; i > paraPosition ; i--){
paraListPtr -> data[i] = paraListPtr -> data[i - 1];
}
paraListPtr -> data[paraPosition] = paraValue;
paraListPtr -> actualLength ++;
}
void sequentialListInsertTest(){
int i ;
int tempArray[5] = {3,5,2,7,4};
printf("--- sequentialListInsertTest begins. --- \r\n");
SequentialListPtr tempList = sequentialListInsert(tempArray , 5);
printf("After initialization , the list is :");
outputList(tempList);
printf("Now insert to the list is :");
sequentialListInsert(tempList , 0 , 8);
outputList(temList);
printf("Now inset to the last , the list is : ");
sequentialListInsert(tempList , 6 , 9 );
printf("The list is :");
outputList(temList);
for(int i = 0 ; i < 5 ; i++){
printf("Inserting %d .\r\n " , (i + 10));
outputList(tempList);
}
printf("--- sequentialListInsertTest ends. --- \r\n");
}
/**
* 顺序表删除元素
* paraList 是顺序表的地址
* paraPosition 是删除位置
* */
int sequentialListDelete(sequentialListPtr pararListPtr , int paraPosition){
// 检查传入顺序表是否长度在 0 到 10
if(paraPosition < 0){
printf("Invalid position : %d. \r\n" , paraPosition);
return -1;
}
if(pararPosition >= paraListPtr -> actualLength){
printf("Cannot delete element : the position %d is beyond the list length %d . \r\n");
return -1;
}
int resultValue = paraListPtr -> data[paraPosition];
// 把 position 后面的元素向前覆盖
for(int i = paraPositon ; i < paraListPtr -> actualLength ; i++){
paraListPtr -> data[i] = paraListPtr -> data[i + 1];
}
paraListPtr -> actualLength --;
return resulValue;
}
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 :");
sequantialListDelete(tempList , 0);
outputList(tempList);
printf("Now delete the last , the list is :");
sequentialListDelete(tempList , 3);
outputList(tempList);
printf("Now delete the 5th ,the list is :");
sequantialListDelete(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);
}
/**
* 定位顺序表元素位置
* paraListPtr 传入顺序表地址
* paraValue 传入元素
* */
int locateElement (SequentialListPtr paraListPtr ,int paraValue){
for(int i = 0 ; i < paraListPtr -> actualLength ; i++){
if(paraListPtr -> data[i] == paraValue){
return i;
}
}
return -1;
}
/**
* 查找顺序表中的元素
* paraListPtr 顺序表的地址
* paraPosition 元素位置
* 返回 -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 positon %d is beyond the list length %d. \r\n");
return -1;
}
return paraListPtr -> data[paraPosition];
}
/**
* 清空顺序表
* paraListPtr 表示顺序表的地址
* */
void clearList(SequentialListPtr paraListPtr){
praraList -> actualLength = 0;
}
void main(){
sequentialInsertTest();
sequentialDeleteTest();
}