数据结构线性顺序表

数据结构线性顺序表

线性顺序表简介

使用顺序存储结构的线性存储结构的表为线性顺序表,线性存储结构是元素逻辑结构一对一,顺序存储结构是元素物理结构连续,表是操作没有限制.

C语言实现代码

#include<stdio.h>//包含标准输入输出库文件
#include<stdlib.h>//包含标准库库文件
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));//顺序表成员数组赋值顺序表成员数组重新分配加加顺序表成员长度乘字节整数
	for(int Index=sequential_list->Length;Index>Insert_Index;--Index)//循环,整数索引赋值顺序表成员长度,索引大于插入索引,减减索引
		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;//减减顺序表成员长度
	for(int Index=Delete_Index;Index<sequential_list->Length;++Index)//循环,整数索引赋值删除索引,索引小于顺序表成员长度,加加索引
		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);//顺序表销毁顺序表
}

C++语言实现代码

#include<iostream>//包含输入输出流库文件
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];//数组赋值新加加长度乘整数
		for(int Index=0;Index<Length;++Index)//循环,整数索引赋值0,索引小于长度,加加索引
			Array[Index]=Temporary_Array[Index];//数组索引下标赋值临时数组索引下标
		delete Temporary_Array;//删除临时数组
		for(int Index=Length-1;Index>Insert_Index;--Index)//循环,整数索引赋值长度减1,索引大于插入索引,减减索引
			Array[Index]=Array[Index-1];//数组索引下标赋值数组索引减1下标
		Array[Insert_Index]=Insert_Data;//数组插入索引下标赋值插入数据
	}
	void Delete(int Delete_Index)//空删除,整数删除索引
	{
		--Length;//减减长度
		for(int Index=Delete_Index;Index<Length;++Index)//循环,整数索引赋值删除索引,索引小于长度,加加索引
			Array[Index]=Array[Index+1];//数组索引下标赋值数组索引加1下标
	}
	int Obtain(int Obtain_Index)//整数获取,整数获取索引
	{
		return Array[Obtain_Index];//返回数组获取索引下标
	}
	int Size(void)//整数尺寸,空
	{
		return Length;//返回长度
	}
}sequential_list;//顺序表
int main(void)//整数主,空
{
	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<<sequential_list.Obtain(Index);//标准输出顺序表成员获取索引数据
		}
		else if(Select==4)//否则如果,选择等于4
			std::cout<<sequential_list.Size();//标准输出顺序表成员尺寸
	}
}
  • 50
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 18
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学编程的闹钟

自愿打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值