網站首頁 編程語言 正文
線性順序表簡介
使用順序存儲結構的線性存儲結構的表為線性順序表,線性存儲結構是元素邏輯結構一對一,順序存儲結構是元素物理結構連續,線性順序表操作沒有限制,線性順序表優點是可以使用下標獲取和修改元素,線性順序表缺點是不可以直接插入和刪除元素.
C語言實現代碼
#include<stdio.h>//包含標準輸入輸出文件
#include<stdlib.h>//包含標準庫文件
typedef struct//定義類型定義結構體
{
int*Array,Length;//定義整數指針變量數組,定義整數變量長度
}Sequential_List;//定義順序表
Sequential_List Sequential_List_Create(void)//順序表創造
{
return(Sequential_List){malloc(0)};//返回順序表數組賦值為分配0字節返回值并且退出函數
}
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_Element/*定義整數變量插入元素*/)//順序表插入
{
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_Element;//順序表數組第插入索引個元素賦值為插入元素
}
void Sequential_List_Delete(Sequential_List*sequential_list/*定義順序表指針變量順序表*/,int Delete_Index/*定義整數變量刪除索引*/)//順序表刪除
{
--sequential_list->Length;//順序表長度累減1
for(int Index=Delete_Index;Index<sequential_list->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_Obtain_Length(Sequential_List sequential_list/*定義順序表變量順序表*/)//順序表獲取長度
{
return sequential_list.Length;//返回順序表長度并且退出函數
}
int main(void)//主函數
{
Sequential_List sequential_list=Sequential_List_Create();//定義順序表變量順序表賦值為順序表創造返回值
int Select,Element,Index;//定義整數變量選擇,定義整數變量元素,定義整數變量索引
do{
scanf("%i",&Select);//格式掃描選擇
if(Select==1)//選擇等于1
{
scanf("%i%i",&Index,&Element);//格式掃描索引和元素
Sequential_List_Insert(&sequential_list,Index,Element);//順序表插入第索引個元素為元素
}
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_Obtain_Length(sequential_list));//格式打印順序表獲取長度返回值
}while(Select);//選擇不等于0
Sequential_List_Destroy(&sequential_list);//順序表銷毀
}
C++語言實現代碼
#include<iostream>//包含輸入輸出流文件
template<typename Type/*類型*/>struct Sequential_List//定義模板結構體順序表
{
Type*Array=new Type;//定義類型指針變量數組賦值為新類型字節返回值
int Length=0;//定義整數變量長度賦值為0
~Sequential_List(void)//順序表析構
{
delete Array;//刪除數組
}
void Insert(int Insert_Index/*定義整數變量插入索引*/,Type Insert_Element/*定義類型變量插入元素*/)//插入
{
Type*temporary_Array=Array;//定義類型指針變量臨時數組賦值為數組
Array=new Type[++Length];//數組賦值為新長度累加1乘類型字節返回值
for(int Index=0;Index<Length;++Index)//定義整數變量索引賦值為0,索引小于長度,索引累加1
Array[Index]=temporary_Array[Index];//數組第索引個元素賦值為臨時數組第索引個元素
delete temporary_Array;//刪除臨時數組
for(int Index=Length-1;Index>Insert_Index;--Index)//定義整數變量索引賦值為長度減1,索引大于插入索引,索引累減1
Array[Index]=Array[Index-1];//數組第索引個元素賦值為數組第索引減1個元素
Array[Insert_Index]=Insert_Element;//數組第插入索引個元素賦值為插入元素
}
void Delete(int Delete_Index/*定義整數變量刪除索引*/)//刪除
{
--Length;//長度累減1
for(int Index=Delete_Index;Index<Length;++Index)//定義整數變量索引賦值為刪除索引,索引小于長度,索引累加1
Array[Index]=Array[Index+1];//數組第索引個元素賦值為數組第索引加1個元素
}
int Obtain(int Obtain_Index/*定義整數變量獲取索引*/)//獲取
{
return Array[Obtain_Index];//返回數組第獲取索引個元素并且退出函數
}
int Obtain_Length(void)//獲取長度
{
return Length;//返回長度并且退出函數
}
};
int main(void)//主函數
{
Sequential_List<int>sequential_list;//定義順序表整數變量順序表
int Select,Element,Index;//定義整數變量選擇,定義整數變量元素,定義整數變量索引
do{
std::cin>>Select;//標準輸入選擇
if(Select==1)//選擇等于1
{
std::cin>>Index>>Element;//標準輸入索引和元素
sequential_list.Insert(Index,Element);//順序表插入第索引個元素為元素
}
else if(Select==2)//選擇等于2
{
std::cin>>Index;//標準輸入索引
sequential_list.Delete(Index);//順序表刪除第索引個元素
}
else if(Select==3)//選擇等于3
{
std::cin>>Index;//標準輸入索引
std::cout<<sequential_list.Obtain(Index);//標準輸出順序表獲取第索引個元素返回值
}
else if(Select==4)//選擇等于4
std::cout<<sequential_list.Obtain_Length();//標準輸出順序表獲取長度返回值
}while(Select);//選擇不等于0
}
原文鏈接:https://blog.csdn.net/vbnetcx/article/details/124243705
相關推薦
- 2022-05-20 python循環控制之break和continue流程控制語句_python
- 2022-07-16 Electron項目打包
- 2022-08-16 .Net使用Xunit工具進行單元測試_實用技巧
- 2022-09-01 C語言全面梳理結構體知識點_C 語言
- 2023-05-15 shell?提取文件名和目錄名的方法實現_linux shell
- 2022-03-14 servlet已經配置url映射,提示Servlet should have a mapping a
- 2023-11-19 Linux虛擬機VMware的Ubuntu使用vi指令的方向鍵和backspace空格鍵亂碼
- 2022-09-09 OpenSSL生成v3證書方法及配置文件詳解_其它綜合
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支