網(wǎng)站首頁 編程語言 正文
線性順序表簡介
使用順序存儲(chǔ)結(jié)構(gòu)的線性存儲(chǔ)結(jié)構(gòu)的表為線性順序表,線性存儲(chǔ)結(jié)構(gòu)是元素邏輯結(jié)構(gòu)一對(duì)一,順序存儲(chǔ)結(jié)構(gòu)是元素物理結(jié)構(gòu)連續(xù),線性順序表操作沒有限制,線性順序表優(yōu)點(diǎn)是可以使用下標(biāo)獲取和修改元素,線性順序表缺點(diǎn)是不可以直接插入和刪除元素.
C語言實(shí)現(xiàn)代碼
#include<stdio.h>//包含標(biāo)準(zhǔn)輸入輸出文件
#include<stdlib.h>//包含標(biāo)準(zhǔn)庫文件
typedef struct//定義類型定義結(jié)構(gòu)體
{
int*Array,Length;//定義整數(shù)指針變量數(shù)組,定義整數(shù)變量長度
}Sequential_List;//定義順序表
Sequential_List Sequential_List_Create(void)//順序表創(chuàng)造
{
return(Sequential_List){malloc(0)};//返回順序表數(shù)組賦值為分配0字節(jié)返回值并且退出函數(shù)
}
void Sequential_List_Destroy(Sequential_List*sequential_list/*定義順序表指針變量順序表*/)//順序表銷毀
{
free(sequential_list->Array);//釋放順序表數(shù)組
}
void Sequential_List_Insert(Sequential_List*sequential_list/*定義順序表指針變量順序表*/,int Insert_Index/*定義整數(shù)變量插入索引*/,int Insert_Element/*定義整數(shù)變量插入元素*/)//順序表插入
{
sequential_list->Array=realloc(sequential_list->Array,++sequential_list->Length*sizeof(int));//順序表數(shù)組賦值為重新分配順序表長度累加1乘整數(shù)字節(jié)返回值
for(int Index=sequential_list->Length;Index>Insert_Index;--Index)//定義整數(shù)變量索引賦值為順序表長度,索引大于插入索引,索引累減1
sequential_list->Array[Index]=sequential_list->Array[Index-1];//順序表數(shù)組第索引個(gè)元素賦值為順序表數(shù)組第索引減1個(gè)元素
sequential_list->Array[Insert_Index]=Insert_Element;//順序表數(shù)組第插入索引個(gè)元素賦值為插入元素
}
void Sequential_List_Delete(Sequential_List*sequential_list/*定義順序表指針變量順序表*/,int Delete_Index/*定義整數(shù)變量刪除索引*/)//順序表刪除
{
--sequential_list->Length;//順序表長度累減1
for(int Index=Delete_Index;Index<sequential_list->Length;++Index)//定義整數(shù)變量索引賦值為刪除索引,索引小于順序表長度,索引累加1
sequential_list->Array[Index]=sequential_list->Array[Index+1];//順序表數(shù)組第索引個(gè)元素賦值為順序表數(shù)組第索引加1個(gè)元素
}
int Sequential_List_Obtain(Sequential_List sequential_list/*定義順序表變量順序表*/,int Obtain_Index/*定義整數(shù)變量獲取索引*/)//順序表獲取
{
return sequential_list.Array[Obtain_Index];//返回順序表數(shù)組第獲取索引個(gè)元素并且退出函數(shù)
}
int Sequential_List_Obtain_Length(Sequential_List sequential_list/*定義順序表變量順序表*/)//順序表獲取長度
{
return sequential_list.Length;//返回順序表長度并且退出函數(shù)
}
int main(void)//主函數(shù)
{
Sequential_List sequential_list=Sequential_List_Create();//定義順序表變量順序表賦值為順序表創(chuàng)造返回值
int Select,Element,Index;//定義整數(shù)變量選擇,定義整數(shù)變量元素,定義整數(shù)變量索引
do{
scanf("%i",&Select);//格式掃描選擇
if(Select==1)//選擇等于1
{
scanf("%i%i",&Index,&Element);//格式掃描索引和元素
Sequential_List_Insert(&sequential_list,Index,Element);//順序表插入第索引個(gè)元素為元素
}
else if(Select==2)//選擇等于2
{
scanf("%i",&Index);//格式掃描索引
Sequential_List_Delete(&sequential_list,Index);//順序表刪除第索引個(gè)元素
}
else if(Select==3)//選擇等于3
{
scanf("%i",&Index);//格式掃描索引
printf("%i",Sequential_List_Obtain(sequential_list,Index));//格式打印順序表獲取第索引個(gè)元素返回值
}
else if(Select==4)//選擇等于4
printf("%i",Sequential_List_Obtain_Length(sequential_list));//格式打印順序表獲取長度返回值
}while(Select);//選擇不等于0
Sequential_List_Destroy(&sequential_list);//順序表銷毀
}
C++語言實(shí)現(xiàn)代碼
#include<iostream>//包含輸入輸出流文件
template<typename Type/*類型*/>struct Sequential_List//定義模板結(jié)構(gòu)體順序表
{
Type*Array=new Type;//定義類型指針變量數(shù)組賦值為新類型字節(jié)返回值
int Length=0;//定義整數(shù)變量長度賦值為0
~Sequential_List(void)//順序表析構(gòu)
{
delete Array;//刪除數(shù)組
}
void Insert(int Insert_Index/*定義整數(shù)變量插入索引*/,Type Insert_Element/*定義類型變量插入元素*/)//插入
{
Type*temporary_Array=Array;//定義類型指針變量臨時(shí)數(shù)組賦值為數(shù)組
Array=new Type[++Length];//數(shù)組賦值為新長度累加1乘類型字節(jié)返回值
for(int Index=0;Index<Length;++Index)//定義整數(shù)變量索引賦值為0,索引小于長度,索引累加1
Array[Index]=temporary_Array[Index];//數(shù)組第索引個(gè)元素賦值為臨時(shí)數(shù)組第索引個(gè)元素
delete temporary_Array;//刪除臨時(shí)數(shù)組
for(int Index=Length-1;Index>Insert_Index;--Index)//定義整數(shù)變量索引賦值為長度減1,索引大于插入索引,索引累減1
Array[Index]=Array[Index-1];//數(shù)組第索引個(gè)元素賦值為數(shù)組第索引減1個(gè)元素
Array[Insert_Index]=Insert_Element;//數(shù)組第插入索引個(gè)元素賦值為插入元素
}
void Delete(int Delete_Index/*定義整數(shù)變量刪除索引*/)//刪除
{
--Length;//長度累減1
for(int Index=Delete_Index;Index<Length;++Index)//定義整數(shù)變量索引賦值為刪除索引,索引小于長度,索引累加1
Array[Index]=Array[Index+1];//數(shù)組第索引個(gè)元素賦值為數(shù)組第索引加1個(gè)元素
}
int Obtain(int Obtain_Index/*定義整數(shù)變量獲取索引*/)//獲取
{
return Array[Obtain_Index];//返回?cái)?shù)組第獲取索引個(gè)元素并且退出函數(shù)
}
int Obtain_Length(void)//獲取長度
{
return Length;//返回長度并且退出函數(shù)
}
};
int main(void)//主函數(shù)
{
Sequential_List<int>sequential_list;//定義順序表整數(shù)變量順序表
int Select,Element,Index;//定義整數(shù)變量選擇,定義整數(shù)變量元素,定義整數(shù)變量索引
do{
std::cin>>Select;//標(biāo)準(zhǔn)輸入選擇
if(Select==1)//選擇等于1
{
std::cin>>Index>>Element;//標(biāo)準(zhǔn)輸入索引和元素
sequential_list.Insert(Index,Element);//順序表插入第索引個(gè)元素為元素
}
else if(Select==2)//選擇等于2
{
std::cin>>Index;//標(biāo)準(zhǔn)輸入索引
sequential_list.Delete(Index);//順序表刪除第索引個(gè)元素
}
else if(Select==3)//選擇等于3
{
std::cin>>Index;//標(biāo)準(zhǔn)輸入索引
std::cout<<sequential_list.Obtain(Index);//標(biāo)準(zhǔn)輸出順序表獲取第索引個(gè)元素返回值
}
else if(Select==4)//選擇等于4
std::cout<<sequential_list.Obtain_Length();//標(biāo)準(zhǔn)輸出順序表獲取長度返回值
}while(Select);//選擇不等于0
}
原文鏈接:https://blog.csdn.net/vbnetcx/article/details/124243705
相關(guān)推薦
- 2022-10-31 理解k8s控制器DaemonSet創(chuàng)建及使用場(chǎng)景_云其它
- 2022-06-06 判斷一個(gè)元素是否在可視區(qū)域中
- 2022-04-29 C++圖解單向鏈表類模板和iterator迭代器類模版詳解_C 語言
- 2022-05-06 Spring Boot 使用 RequiredArgsConstructor 參數(shù) onConstr
- 2022-08-16 Python利用fastapi實(shí)現(xiàn)上傳文件_python
- 2022-09-03 C++11?condition_variable條件變量的用法說明_C 語言
- 2023-04-18 Python之split函數(shù)的深入理解_python
- 2022-05-14 Python學(xué)習(xí)之裝飾器與類的裝飾器詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支