数据结构-顺序表

#include<bits/stdc++.h> 
using namespace std;
typedef int ElemType;

// 静态分配 
#define MaxSize 10
typedef struct{
	ElemType data[MaxSize];
	ElemType length;
}SqList;


/*//动态分配 
#define InitSize 10
typedef struct{
	ElemType *data;
	int MaxSize,length;
}SqList;

void InitList(SqList &L)
{
	L.data=(ElemType *)malloc(sizeof(ElemType)*InitSize);
	L.length=0;
	L.MaxSize=InitSize;
}
*/

// 静态 
void InitList(SqList &L){
	for(int i=0;i<MaxSize;i++)
	{
		L.data[i]=0; 
	}
	L.length=0; 
}


bool ListInsert(SqList &L,int i,ElemType e)
{
	if(i<1||i>L.length+1) return false;
	if(L.length>=MaxSize) return false;
	for(int j=L.length;j>=i;j--)
	{
		L.data[j]=L.data[j-1];
	}
	L.data[i-1]=e;
	L.length++;
	return true;
}
//    0 1 2 3 4 5 6 7 8 9 Length=10;L-1=9; 
   // 1 2 3 4 5 6 7 8 9 10
bool ListDelete(SqList &L,int i,ElemType e)
{
	if(i<1||i>L.length) return false;
	e=L.data[i-1];
	for(int j=i;j<L.length;j++)
	{
		L.data[j-1]=L.data[j];
	}
	L.length--;
	return true;
}

int LocateElem(SqList L,ElemType e)
{
	for(int i=0;i<L.length;i++)
	{
		if(L.data[i]==e)
		return i+1;
	}
	return 0;
}

int main()
{
	SqList L;
	InitList(L);
	for(int i=0;i<MaxSize;i++)
	{
		cout<<"L.data["<<i<<"]="<<L.data[i]<<endl;
	}
	int e;
	ListInsert(L,1,1);
	ListInsert(L,2,2);
	ListInsert(L,2,2);
	ListInsert(L,4,4);
	ListDelete(L,3,e); 
	cout<<LocateElem(L,4);
	cout<<endl;
	for(int i=0;i<L.length;i++)
	{
		cout<<"L.data["<<i<<"]="<<L.data[i]<<endl;
	}
	return 0;
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值