顺序表数据结构及基本运算

1、定义

    借组数组开辟地址连续的存储空间。

2、特点

    1、地址连续的存储单元

    2、最简单的,最自然的存储方法。

3、数据结构描述

#define MAXSIZE 100
typedef struct
{
    int data[MAXSIZE];
    int last;
}sequencelist;

sequencelist *seqlist;

4、基本运算

查找、增加、删除

//find x location
int find_elem(sequencelist *L, int x)
{
	assert(L != NULL);
	for(int i = 1; i < L->last; i++)
	{
		if(L->data[i] == x)
		{
			return i;
		}
	}
	return 0;
}
// 在第pos位置加入元素x
int add_elem(sequencelist *L, int x, int pos)
{
	assert(L != NULL);
	if(L->last >= MAXSIZE)//data 范围【1,MAXSIZE】
	{
		return LIST_OVERFLOW_ERR;
	}
	else(pos < 1 || pos > L->last)
	{
		return ADD_POS_ERR;
	}
	else
	{
		for(int j = L->last+1; j > pos; j--)
		{
			L->data[j] = L->data[j-1];
		}
		L->data[pos] = x;
		L->last = L->last++;
	}
	return RET_SUCCESS;
}

// delete elem
int del_elem(sequencelist *L, int pos)
{
	assert(L != NULL);
	if(L->last == 0)
	{
		return NULL_LIST_ERR;
	}
	else
	{	
		for(int i = pos; i >= L->last; i++)
		{
			L->data[i] = L->data[i+1];
		}
		L->last--;
		return RET_SUCCESS;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值