欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C/C++實(shí)現(xiàn)線性順序表的示例代碼

 更新時(shí)間:2022年05月27日 11:00:12   作者:學(xué)編程的鬧鐘  
使用順序存儲(chǔ)結(jié)構(gòu)的線性存儲(chǔ)結(jié)構(gòu)的表為線性順序表。本文將分別利用C語(yǔ)言和C++實(shí)現(xiàn)線性順序表,文中示例代碼講解詳細(xì),需要的可以參考一下

線性順序表簡(jiǎn)介

使用順序存儲(chǔ)結(jié)構(gòu)的線性存儲(chǔ)結(jié)構(gòu)的表為線性順序表,線性存儲(chǔ)結(jié)構(gòu)是元素邏輯結(jié)構(gòu)一對(duì)一,順序存儲(chǔ)結(jié)構(gòu)是元素物理結(jié)構(gòu)連續(xù),線性順序表操作沒(méi)有限制,線性順序表優(yōu)點(diǎn)是可以使用下標(biāo)獲取和修改元素,線性順序表缺點(diǎn)是不可以直接插入和刪除元素.

C語(yǔ)言實(shí)現(xiàn)代碼

#include<stdio.h>//包含標(biāo)準(zhǔn)輸入輸出文件
#include<stdlib.h>//包含標(biāo)準(zhǔn)庫(kù)文件
typedef struct//定義類型定義結(jié)構(gòu)體
{
	int*Array,Length;//定義整數(shù)指針變量數(shù)組,定義整數(shù)變量長(zhǎng)度
}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ù)組賦值為重新分配順序表長(zhǎng)度累加1乘整數(shù)字節(jié)返回值
	for(int Index=sequential_list->Length;Index>Insert_Index;--Index)//定義整數(shù)變量索引賦值為順序表長(zhǎng)度,索引大于插入索引,索引累減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;//順序表長(zhǎng)度累減1
	for(int Index=Delete_Index;Index<sequential_list->Length;++Index)//定義整數(shù)變量索引賦值為刪除索引,索引小于順序表長(zhǎng)度,索引累加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/*定義順序表變量順序表*/)//順序表獲取長(zhǎng)度
{
	return sequential_list.Length;//返回順序表長(zhǎng)度并且退出函數(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));//格式打印順序表獲取長(zhǎng)度返回值
	}while(Select);//選擇不等于0
	Sequential_List_Destroy(&sequential_list);//順序表銷毀
}

C++語(yǔ)言實(shí)現(xiàn)代碼

#include<iostream>//包含輸入輸出流文件
template<typename Type/*類型*/>struct Sequential_List//定義模板結(jié)構(gòu)體順序表
{
	Type*Array=new Type;//定義類型指針變量數(shù)組賦值為新類型字節(jié)返回值
	int Length=0;//定義整數(shù)變量長(zhǎng)度賦值為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ù)組賦值為新長(zhǎng)度累加1乘類型字節(jié)返回值
		for(int Index=0;Index<Length;++Index)//定義整數(shù)變量索引賦值為0,索引小于長(zhǎng)度,索引累加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ù)變量索引賦值為長(zhǎng)度減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;//長(zhǎng)度累減1
		for(int Index=Delete_Index;Index<Length;++Index)//定義整數(shù)變量索引賦值為刪除索引,索引小于長(zhǎng)度,索引累加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)//獲取長(zhǎng)度
	{
		return Length;//返回長(zhǎng)度并且退出函數(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)輸出順序表獲取長(zhǎng)度返回值
	}while(Select);//選擇不等于0
}

到此這篇關(guān)于C/C++實(shí)現(xiàn)線性順序表的示例代碼的文章就介紹到這了,更多相關(guān)C++線性順序表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論