数据结构顺序链表的创建插入及相关操作

#include<iostream>
using namespace std;
#define MAXSIZE 100
#define ERROR 0
typedef struct
{
	int *elem;
	int length;
}List;


void creatList(List &L)
{
	L.elem=new int[MAXSIZE];
	L.length=0;
	return;
}


void inputList(List &L,int n)
{
	int i=0;
	for(i;i<n;i++)
	{
		cout<<"请输入第"<<i+1<<"个数据:";
		cin>>L.elem[i];
	}
	L.length=i;
	return; 
}


int Listlength(List L)
{
	return L.length;
}


int Getelem(List L,int i)
{
    return L.elem[i-1];
}


void Insertlist(List &L,int e,int i)
{
	for(int j=L.length-1;j>=i-1;j--)
	{
		L.elem[j+1]=L.elem[j];
	}
	L.elem[i-1]=e;
	++L.length;
	return;
}


void Cancellist(List &L,int i)
{
	int j=i-1;
	for(j;j<L.length;j++)
	{
		L.elem[j]=L.elem[j+1];
	}
	--L.length;
	return;
}


void Output(List &L)
{
	cout<<"顺序表为:";
	for(int i=0;i<L.length;i++)
	{
		cout<<L.elem[i]<<" ";
	}
	cout<<endl;
	return;
}


int main()
{
	List L;
	while(1)
	{
		cout<<"*****************************************************"<<endl;
		cout<<"******1.顺序表初始化         2.顺序表数据填充********"<<endl; 
		cout<<"******3.顺序表的长度         4.顺序表的查找**********"<<endl;
		cout<<"******5.顺序表的插入         6.顺序表的删除**********"<<endl;
		cout<<"******7.顺序表的显示         8.退出程序    **********"<<endl; 
		int k;
		cout<<"输入你的选择:";
		cin>>k;
		switch(k)
		{
		case 1:
			creatList(L);
			cout<<"顺序表初始化成功!"<<endl;
			continue;
		case 2:
			int n;
			cout<<"填充数据的个数:";
			cin>>n;
			inputList(L,n);
			cout<<"数据填充成功!"<<endl;
			continue;
		case 3:
			cout<<"顺序表的长度为:"<<Listlength(L)<<endl;
			continue;
		case 4:
			int i;
			cout<<"查找数据的位置为:";
			cin>>i;
			cout<<"该数据为:"<<Getelem(L,i)<<endl;
			continue;
		case 5:
			int e;
			int j;
			cout<<"插入的数据为:";
			cin>>e;
			cout<<"插入的位置为:";
			cin>>j;
			Insertlist(L,e,j);
			cout<<"顺序表插入成功!";
			continue;
		case 6:
			int a;
			cout<<"删除的位置为:";
			cin>>a;
			Cancellist(L,a);
			cout<<"数据删除成功!";
			continue;
		case 7:
			Output(L);
			continue;
		case 8:
			break;
		default:
			cout<<"请输入正确的选项!"<<endl;
			system("pause");
			continue;
		}
		break;
	}
	return 0;
 } 

本代码取用的是C++来进行编译。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

夜雨时’

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值