数据结构线性顺序表 线性顺序表简介
使用顺序存储结构的线性存储结构的表为线性顺序表,线性存储结构是元素逻辑结构一对一,顺序存储结构是元素物理结构连续,线性顺序表操作没有限制,线性顺序表优点是可以使用下标获取和修改元素,线性顺序表缺点是不可以直接插入和删除元素.
C语言实现代码#includeC++语言实现代码//包含标准输入输出库文件 #include //包含标准库库文件 typedef struct//类型定义结构体 { int*Array,Length;//整数指针数组,整数长度 }Sequential_List;//顺序表 Sequential_List Sequential_List_Create(void)//顺序表创造 { return(Sequential_List){malloc(sizeof(int))};//返回顺序表数组赋值为分配整数字节并且退出函数 } void Sequential_List_Destroy(Sequential_List*sequential_list)//顺序表销毁,顺序表指针顺序表 { free(sequential_list->Array);//释放顺序表数组 } void Sequential_List_Insert(Sequential_List*sequential_list,int Insert_Index,int Insert_Data)//顺序表插入,顺序表指针顺序表,整数插入索引,整数插入数据 { sequential_list->Array=realloc(sequential_list->Array,++sequential_list->Length*sizeof(int));//顺序表数组赋值为顺序表数组重新分配顺序表长度前缀累加1乘整数字节 for(int Index=sequential_list->Length;Index>Insert_Index;--Index)//整数索引赋值为顺序表长度,索引大于插入索引,索引前缀累减1 sequential_list->Array[Index]=sequential_list->Array[Index-1];//顺序表数组第索引个元素数据赋值为顺序表数组第索引减1个元素数据 sequential_list->Array[Insert_Index]=Insert_Data;//顺序表数组第插入索引个元素数据赋值为插入数据 } void Sequential_List_Delete(Sequential_List*sequential_list,int Delete_Index)//顺序表删除,顺序表指针顺序表,整数删除索引 { --sequential_list->Length;//顺序表长度前缀累减1 for(int Index=Delete_Index;Index Length;++Index)//整数索引赋值为删除索引,索引小于顺序表长度,索引前缀累加1 sequential_list->Array[Index]=sequential_list->Array[Index+1];//顺序表数组第索引个元素数据赋值为顺序表数组第索引加1个元素数据 } int Sequential_List_Obtain(Sequential_List sequential_list,int Obtain_Index)//顺序表获取,顺序表顺序表,整数获取索引 { return sequential_list.Array[Obtain_Index];//返回顺序表数组第获取索引个元素数据并且退出函数 } int Sequential_List_Size(Sequential_List sequential_list)//顺序表尺寸,顺序表顺序表 { return sequential_list.Length;//返回顺序表长度并且退出函数 } int main(void)//主函数 { Sequential_List sequential_list=Sequential_List_Create();//顺序表顺序表赋值为顺序表创造 for(int Select=5,Data,Index;Select;scanf("%i",&Select))//整数选择赋值为5,整数数据,整数索引,选择不等于0,格式扫描选择 { if(Select==1)//选择等于1 { scanf("%i%i",&Index,&Data);//格式扫描索引和数据 Sequential_List_Insert(&sequential_list,Index,Data);//顺序表插入第索引个元素数据为数据 } else if(Select==2)//选择等于2 { scanf("%i",&Index);//格式扫描索引 Sequential_List_Delete(&sequential_list,Index);//顺序表删除第索引个元素数据 } else if(Select==3)//选择等于3 { scanf("%i",&Index);//格式扫描索引 printf("%i",Sequential_List_Obtain(sequential_list,Index));//格式打印顺序表获取第索引个元素数据 } else if(Select==4)//选择等于4 printf("%i",Sequential_List_Size(sequential_list));//格式打印顺序表尺寸 } Sequential_List_Destroy(&sequential_list);//顺序表销毁 }
#include//包含输入输出流库文件 struct Sequential_List//结构体顺序表 { int*Array=new int,Length=0;//整数指针数组赋值为新整数字节,整数长度赋值为0 ~Sequential_List(void)//顺序表析构 { delete Array;//删除数组 } void Insert(int Insert_Index,int Insert_Data)//插入,整数插入索引,整数插入数据 { int*temporary_Array=Array;//整数指针临时数组赋值为数组 Array=new int[++Length];//数组赋值为新长度前缀累加1乘整数字节 for(int Index=0;Index Insert_Index;--Index)//整数索引赋值为长度减1,索引大于插入索引,索引前缀累减1 Array[Index]=Array[Index-1];//数组第索引个元素数据赋值为数组第索引减1个元素数据 Array[Insert_Index]=Insert_Data;//数组第插入索引个元素数据赋值为插入数据 } void Delete(int Delete_Index)//删除,整数删除索引 { --Length;//长度前缀累减1 for(int Index=Delete_Index;Index Length;//返回长度并且退出函数 } }; int main(void)//主函数 { Sequential_List sequential_list;//顺序表顺序表 for(int Select=5,Data,Index;Select;std::cin>>Select)//整数选择赋值为5,整数数据,整数索引,选择不等于0,标准输入选择 { if(Select==1)//选择等于1 { std::cin>>Index>>Data;//标准输入索引和数据 sequential_list.Insert(Index,Data);//顺序表插入第索引个元素数据为数据 } else if(Select==2)//选择等于2 { std::cin>>Index;//标准输入索引 sequential_list.Delete(Index);//顺序表删除第索引个元素数据 } else if(Select==3)//选择等于3 { std::cin>>Index;//标准输入索引 std::cout< 编程交流群群号(QQ):387618421



