顺序表
介绍顺序表前先来了解一下
顺序结构
:
将表中元素一个接一个的存入一组连续的
存储单元
中,这种
存储结构
是
顺序结构
。而采用
顺序存储结构
的线性表简称为“ 顺序表”。
顺序表的基本操作:创建顺序表,初始化顺序表,数据元素的插入,删除某个数据元素,遍历顺序表,
顺序表的存储特点
- 逻辑上相邻的元素,物理上也相邻
- 若已知表中首元素在存储器中的位置,则其他元素存放位置亦可通过公式得到:
LOC(ai)=LOC(a1)+(i-1)*L
1≤i≤n
其中,L是元素占用存储单元的长度求出。
顺序表的实现
预定义的一些参数
#define LIST_INIT_SIZE 100 //最初分配的大小
#define LISTINCREMENT 10 //每次增加的数据元素数量
#define ELEMTYPE int //顺序表数据元素的类型
#define ERROR 1
#define TRUE 1
#define FALSE 0
#define INFEASIBLE -1
#define OVERFLOW -2 //溢出
定义结构
typedef struct sequenceList
{
ELEMTYPE *elem;
int length; //当前顺序表的长度
int size; //顺序表的全部大小
}sequenceList;
顺序表的基本操作
- 创建顺序表并初始化
void createSequenceList(sequenceList *L) //创建一个空的顺序表 { L->elem = (ELEMTYPE *)malloc(LIST_INIT_SIZE * sizeof(ELEMTYPE)); //申请一个固定大小的栈空间 if (!L->elem) exit(OVERFLOW); //空间申请失败 L->length = 0; L->size = LIST_INIT_SIZE; }
- 为顺序表赋值
void initSequenceList(sequenceList *L, ELEMTYPE *arr, int num) { while (num > L->size) //当空间不够时,自动分配空间 { L->elem = (ELEMTYPE *)realloc(L->elem, (LIST_INIT_SIZE * LISTINCREMENT) * sizeof(ELEMTYPE)); if (!L->elem) exit(OVERFLOW); } //赋值 int i; for (i = 0; i < num; i++) { *(L->elem+i) = *(arr+i); L->length++; } }
- 插入
int insertSequenceList(sequenceList *L, int i, ELEMTYPE n) { ELEMTYPE *newBase; ELEMTYPE *p; ELEMTYPE *q; if (i < 1 || i > (L->length + 1)) //检测错误位置插入 exit(OVERFLOW); if (L->length >= L->size) //当当前总空间不够时自动分配新的空间 { newBase = (ELEMTYPE *)realloc(L->elem, (LIST_INIT_SIZE + LISTINCREMENT)*sizeof(ELEMTYPE)); if (!newBase) printf("Wrong!!!\n"); L->elem = newBase; L->size += LISTINCREMENT; } p = &L->elem[i-1]; for (q = &L->elem[L->length-1]; q >= p; q--) //将I位之后的值往后移位 *(q+1) = *q; L->elem[i-1] = n; L->length++; return TRUE; }
- 查询
int searchSequenceListItemPosition(sequenceList *L, ELEMTYPE target) { int i; for (i = 0; i < L->length; i++) { if (*(L->elem + i) == target) { return ++i; } } //如果没有匹配项,返回ERROR return ERROR; }
- 删除
int rmItem(sequenceList *L, int i) { ELEMTYPE *p; ELEMTYPE *q; if (i < 1 || i > L->length) { return ERROR; } p = &L->elem[i-1]; q = &L->elem[L->length - 1]; for (++p; p <= q; ++p) *(p-1) = *p; --L->length; return TRUE; }
- 排序
void sortByl2b(sequenceList *L) //O(n^2) { int i,j; ELEMTYPE tmp; for (i = 0; i < L->length; i++) { for (j = i+1; j < L->length; j++) { if (L->elem[i] > L->elem[j]) { tmp = L->elem[i]; L->elem[i] = L->elem[j]; L->elem[j] = tmp; } } } }
- 遍历顺序表
void traverseSequenceList(sequenceList *L) { int i; for (i = 0; i < L->length; i++) { printf("%d \t", L->elem[i]); } printf("\n"); }
- 销毁顺序表
void destroySequenceList(sequenceList *L) { free(L->elem); L->elem = NULL; L->size = 0; L->length = 0; }
最后送上主函数的测试
int main()
{
sequenceList sqL;
int arr[10] = {9,8,7,6,5,4,3,2,1,0};
int position;
createSequenceList(&sqL);
initSequenceList(&sqL, arr, 10);
position = searchSequenceListItemPosition(&sqL, 9);
if (position != ERROR)
{
printf("target'position is %d \n", position);
}
traverseSequenceList(&sqL);
insertSequenceList(&sqL, 1, 10);
insertSequenceList(&sqL, 2, 11);
insertSequenceList(&sqL, 3, 12);
insertSequenceList(&sqL, 4, 13);
insertSequenceList(&sqL, 5, 14);
traverseSequenceList(&sqL);
rmItem(&sqL, 2);
traverseSequenceList(&sqL);
sortByl2b(&sqL);
traverseSequenceList(&sqL);
destroySequenceList(&sqL);
if (sqL.elem == NULL)
printf("SequenceList has been destroyed!\n");
return 0;
}
以上为本人对于顺序表的一些理解与实现,如若你又好的想法和建议,欢迎加我好友一起探讨,共同进步。希望这可以对你理解顺序表有些帮助。加油!
参考书目:
《数据结构(C语言版)》