目录
线性表是n个具有相同特征的数据元素的有限序列。在逻辑上呈现线性结构,但是物理上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般采用数组存储。在数组上完成数据的增删查改。
具体以代码形式呈现
- #pragma once
- #include
- #include
- #include
-
- typedef int SLDataType;
- typedef struct SeqList
- {
- SLDataType* a;
- int size; // 记录存储多少个有效数据
- int capacity; // 空间容量大小
- }SL;
-
- void SLPrint(SL* ps);
- void SLInit(SL* ps);
- void SLDestroy(SL* ps);
- void SLCheckCapacity(SL* ps);
-
- // 尾插尾删
- void SLPushBack(SL* ps, SLDataType x);
- void SLPopBack(SL* ps);
-
- // 头插头删
- void SLPushFront(SL* ps, SLDataType x);
- void SLPopFront(SL* ps);
-
- // 中间插入删除
- // 在pos位置插入数据
- void SLInsert(SL* ps, int pos, SLDataType x);
-
- // 删除pos位置数据
- void SLErase(SL* ps, int pos);
-
- // begin查找x的起始位置
- int SLFind(SL* ps, SLDataType x, int begin);
- #include"SeqList.h"
-
-
- void SLPrint(SL* ps)
- {
- assert(ps);
-
- for (int i = 0; i < ps->size; ++i)
- {
- printf("%d ", ps->a[i]);
- }
- printf("\n");
- }
-
- void SLCheckCapacity(SL* ps)
- {
- assert(ps);
-
- if (ps->size == ps->capacity)
- {
- int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
- SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapacity * sizeof(SLDataType));
- if (tmp == NULL)
- {
- perror("realloc fail");
- exit(-1);
- }
-
- ps->a = tmp;
- ps->capacity = newCapacity;
- }
- }
-
- void SLInit(SL* ps)
- {
- assert(ps);
-
- ps->a = NULL;
- ps->size = 0;
- ps->capacity = 0;
- }
-
- void SLDestroy(SL* ps)
- {
- assert(ps);
-
- //if (ps->a != NULL)
- if (ps->a)
- {
- free(ps->a);
- ps->a = NULL;
- ps->size = ps->capacity = 0;
- }
- }
-
- // O(1)
- void SLPushBack(SL* ps, SLDataType x)
- {
- /*assert(ps);
- SLCheckCapacity(ps);
- ps->a[ps->size] = x;
- ps->size++;*/
-
- SLInsert(ps, ps->size, x);
- }
-
- void SLPopBack(SL* ps)
- {
- //assert(ps);
-
- 温柔的检查
- ///*if (ps->size == 0)
- //{
- //return;
- //}*/
-
- 暴力的检查
- //assert(ps->size > 0);
-
- ps->a[ps->size - 1] = 0;
- //ps->size--;
-
- SLErase(ps, ps->size - 1);
- }
-
- // O(N)
- void SLPushFront(SL* ps, SLDataType x)
- {
- //assert(ps);
- //SLCheckCapacity(ps);
-
- 挪动数据
- //int end = ps->size - 1;
- //while (end >= 0)
- //{
- // ps->a[end + 1] = ps->a[end];
- // end--;
- //}
-
- //ps->a[0] = x;
- //ps->size++;
-
- SLInsert(ps, 0, x);
- }
-
- void SLPopFront(SL* ps)
- {
- /*assert(ps);
- assert(ps->size > 0);
- int begin = 1;
- while (begin < ps->size)
- {
- ps->a[begin - 1] = ps->a[begin];
- begin++;
- }
- ps->size--;*/
-
- SLErase(ps, 0);
- }
-
- // 在pos位置插入数据
- void SLInsert(SL* ps, int pos, SLDataType x)
- {
- assert(ps);
- assert(pos >= 0);
- assert(pos <= ps->size);
-
- SLCheckCapacity(ps);
- int end = ps->size - 1;
- while (end >= pos)
- {
- ps->a[end + 1] = ps->a[end];
- end--;
- }
-
- ps->a[pos] = x;
- ps->size++;
- }
-
- // 删除pos位置数据
- void SLErase(SL* ps, int pos)
- {
- assert(ps);
- assert(pos >= 0);
- assert(pos < ps->size);
- //assert(ps->size > 0);
-
- // 挪动数据覆盖
- int begin = pos + 1;
- while (begin < ps->size)
- {
- ps->a[begin - 1] = ps->a[begin];
- begin++;
- }
-
- ps->size--;
- }
-
- int SLFind(SL* ps, SLDataType x, int begin)
- {
- assert(ps);
-
- for (int i = begin; i < ps->size; ++i)
- {
- if (ps->a[i] == x)
- {
- return i;
- }
- }
-
- return -1;
- }
之前的通讯录本身与顺序表相似,不过顺序表重点在于头插头删,尾插尾删,指定位置增删功能。尾部操作都是直接改变size来实现,而头部两个操作异曲同工,需要一个个挪动元素。指定位置增删也相似,增加右移,留出pos位置,然后添加。删除则是左移覆盖过去。当实现pos位置增删后,头尾插删其实也就可以完全用这两个函数来代替。
结束。