1、线性表的概念
1.1线性表
线性表(linear list)是n个具有相同特性的数据元素的有限序列。线性表是⼀种在实际中⼴泛使
⽤的数据结构,常⻅的线性表:顺序表、链表、栈、队列、字符串...
线性表在逻辑上是线性结构,也就说是连续的⼀条直线。但是在物理结构上并不⼀定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。(顺序表逻辑结构线性的,物理结构也是连续的)
2、顺序表分类
2.1 顺序表和数组的区别
顺序表的底层结构是数组,对数组的封装,实现了常⽤的增删改查等接⼝
2.2. 顺序表分类
◦ 静态顺序表
概念:使⽤定⻓数组存储元素
缺陷:空间给少了不够⽤,给多了造成空间浪费
#define N 100 //方便后期对数组空间大小进行修改
typedef int SLData; //方便后期对数组元素类型进行修改
//静态顺序表
struct SeqList {
SLData data[N]; //定长数组(这里的N指的是给N个大小的空间,并不是说里面已经就有N个有效数据)
int size; //有效数据个数
};
◦ 动态顺序表
//动态顺序表
struct SeqList {
SLData* arr; //存储数据的底层结构
int capacity; //记录顺序表的空间大小
int size; //有效数据个数
};
3、动态顺序表的实现
创建头文件(.h) 和源文件(.c)
头文件SeqList.h:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define N 100 //方便后期对数组空间大小进行修改
typedef int SLData; //方便后期对数组元素类型进行修改
//静态顺序表
//struct SeqList {
// SLData data[N]; //定长数组(这里的N指的是给N个大小的空间,并不是说里面已经就有N个有效数据)
// int size; //有效数据个数
//};
//动态顺序表
typedef struct SeqList {
SLData* arr; //存储数据的底层结构
int capacity; //记录顺序表的空间大小
int size; //有效数据个数
}SL;
//初始化和销毁
void SLInit(SL* ps);
void SLDestroy(SL* ps);
//打印
void SLPrint(SL* ps);
//顺序表的插入 头插/尾插
void SLPushFront(SL* ps, SLData x);
void SLPushBack(SL* ps, SLData x);
//顺序表的删除
void SLPopBack(SL* ps);
void SLPopFront(SL* ps);
//指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLData x);
//删除指定位置数据
void SLErase(SL* ps, int pos);
//查找指定元素
void SLFind(SL* ps,SLData x);
源文件SeqList.c:
#include"SeqList.h" //初始化和销毁 void SLInit(SL* ps) { ps->arr = NULL; ps->size = ps->capacity = 0; } void SLDestroy(SL* ps) { assert(ps); if(ps->arr){ free(ps->arr); } ps->arr=NULL; ps->size = ps->capacity = 0; } void SLPrint(SL* ps) { for (int i = 0; i < ps->size; i++) { printf("%d ", ps->arr[i]); } printf("\n"); } void SLCheckCapacity(SL* ps) { //断言--头文件(#include<assert.h>) assert(ps != NULL); //assert(ps); /*if (ps == NULL) { return; }*/ if (ps->size == ps->capacity) { int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity; SLData* tmp = (SLData*)realloc(ps->arr, newCapacity * sizeof(SLData)); if (tmp == NULL) //扩容失败 { perror("realloc fail!"); exit(1); } //扩容成功 ps->arr = tmp; ps->capacity = newCapacity; } } //头插、尾插 void SLPushBack(SL* ps, SLData x) { //空间不够扩容 SLCheckCapacity(ps); //空间足够 直接插入 ps->arr[ps->size] = x; ps->size++; } void SLPushFront(SL* ps, SLData x) { assert(ps); //判断是否需要扩容 SLCheckCapacity(ps); //旧数据往后挪一位 for (int i = ps->size; i > 0; i--) { ps->arr[i]= ps->arr[i-1]; } ps->arr[0] = x; ps->size++; } //头删、尾删 void SLPopBack(SL* ps) { //顺序表为空--不能删除 assert(ps); assert(ps->size); //顺序表不为空 ps->size--; } void SLPopFront(SL* ps) { //顺序表为空--不能删除 assert(ps); assert(ps->size); //顺序表不为空 for (int i = 0; i<ps->size-1; i++) { ps->arr[i] = ps->arr[i+1]; } ps->size--; } //指定位置之前插入数据 void SLInsert(SL* ps, int pos, SLData x) { assert(ps); assert(pos >= 0 && pos <= ps->size); SLCheckCapacity(ps); for (int i = ps->size; i >pos; i--){ ps->arr[i] = ps->arr[i - 1]; } ps->arr[pos] = x; ps->size++; } //删除指定位置数据 void SLErase(SL* ps, int pos) { assert(ps); assert(pos >= 0 && pos < ps->size); for (int i = pos; i <ps->size-1; i++) { ps->arr[i] = ps->arr[i + 1]; } ps->size--; } //查找 void SLFind(SL* ps,SLData x){ assert(ps); for(int i=0;i<ps->size;i++) { if(ps->arr[i]==x) { return i; } else { return -1; } } }
#include"SeqList.h" void slTest01() { SL sl; SLInit(&sl); //测试尾插 SLPushBack(&sl,1); SLPushBack(&sl,2); SLPushBack(&sl,3); SLPushBack(&sl,4); /*SLPrint(&sl); SLPushBack(&sl, 5); SLPushBack(NULL, 5); SLPrint(&sl);*/ /*SLPushFront(&sl, 5); SLPushFront(&sl, 6); SLPushFront(&sl, 7); SLPrint(&sl); SLPopBack(&sl); SLPopBack(&sl); SLPrint(&sl);*/ /*SLPopFront(&sl); SLPopFront(&sl); SLPrint(&sl);*/ /*SLInsert(&sl, 3, 5); SLInsert(&sl, 0, 5); SLPrint(&sl);*/ SLErase(&sl, 3); SLErase(&sl, 0); ....... SLPrint(&sl); } int main() { slTest01(); return 0; }