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;
}
}