C/C++實現(xiàn)線性順序表的示例代碼
線性順序表簡介
使用順序存儲結(jié)構(gòu)的線性存儲結(jié)構(gòu)的表為線性順序表,線性存儲結(jié)構(gòu)是元素邏輯結(jié)構(gòu)一對一,順序存儲結(jié)構(gòu)是元素物理結(jié)構(gòu)連續(xù),線性順序表操作沒有限制,線性順序表優(yōu)點是可以使用下標(biāo)獲取和修改元素,線性順序表缺點是不可以直接插入和刪除元素.
C語言實現(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ù)組第索引個元素賦值為順序表數(shù)組第索引減1個元素
sequential_list->Array[Insert_Index]=Insert_Element;//順序表數(shù)組第插入索引個元素賦值為插入元素
}
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ù)組第索引個元素賦值為順序表數(shù)組第索引加1個元素
}
int Sequential_List_Obtain(Sequential_List sequential_list/*定義順序表變量順序表*/,int Obtain_Index/*定義整數(shù)變量獲取索引*/)//順序表獲取
{
return sequential_list.Array[Obtain_Index];//返回順序表數(shù)組第獲取索引個元素并且退出函數(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);//順序表插入第索引個元素為元素
}
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++語言實現(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ù)組
Array=new Type[++Length];//數(shù)組賦值為新長度累加1乘類型字節(jié)返回值
for(int Index=0;Index<Length;++Index)//定義整數(shù)變量索引賦值為0,索引小于長度,索引累加1
Array[Index]=temporary_Array[Index];//數(shù)組第索引個元素賦值為臨時數(shù)組第索引個元素
delete temporary_Array;//刪除臨時數(shù)組
for(int Index=Length-1;Index>Insert_Index;--Index)//定義整數(shù)變量索引賦值為長度減1,索引大于插入索引,索引累減1
Array[Index]=Array[Index-1];//數(shù)組第索引個元素賦值為數(shù)組第索引減1個元素
Array[Insert_Index]=Insert_Element;//數(shù)組第插入索引個元素賦值為插入元素
}
void Delete(int Delete_Index/*定義整數(shù)變量刪除索引*/)//刪除
{
--Length;//長度累減1
for(int Index=Delete_Index;Index<Length;++Index)//定義整數(shù)變量索引賦值為刪除索引,索引小于長度,索引累加1
Array[Index]=Array[Index+1];//數(shù)組第索引個元素賦值為數(shù)組第索引加1個元素
}
int Obtain(int Obtain_Index/*定義整數(shù)變量獲取索引*/)//獲取
{
return Array[Obtain_Index];//返回數(shù)組第獲取索引個元素并且退出函數(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);//順序表插入第索引個元素為元素
}
else if(Select==2)//選擇等于2
{
std::cin>>Index;//標(biāo)準(zhǔn)輸入索引
sequential_list.Delete(Index);//順序表刪除第索引個元素
}
else if(Select==3)//選擇等于3
{
std::cin>>Index;//標(biāo)準(zhǔn)輸入索引
std::cout<<sequential_list.Obtain(Index);//標(biāo)準(zhǔn)輸出順序表獲取第索引個元素返回值
}
else if(Select==4)//選擇等于4
std::cout<<sequential_list.Obtain_Length();//標(biāo)準(zhǔn)輸出順序表獲取長度返回值
}while(Select);//選擇不等于0
}到此這篇關(guān)于C/C++實現(xiàn)線性順序表的示例代碼的文章就介紹到這了,更多相關(guān)C++線性順序表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實現(xiàn)約瑟夫環(huán)的循環(huán)單鏈表
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)約瑟夫環(huán)的循環(huán)單鏈表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
C++開發(fā)之PugiXML庫基礎(chǔ)用法示例詳解
PugiXML庫是一個功能強大、簡單易用的C++ XML解析庫,它提供了一組方便的函數(shù)來解析、創(chuàng)建和修改XML文檔,本文介紹了如何使用PugiXML庫來解析、創(chuàng)建和修改XML文檔,以及如何處理錯誤和異常,感興趣的朋友跟隨小編一起看看吧2024-03-03

