《数据结构世界的乐高积木:顺序表的奇幻旅程》
目录
1. 线性表
2. 顺序表
2.1 概念与结构
2.2 分类
2.2.1 静态顺序表
2.2.2 动态顺序表
2.3 动态顺序表的实现
1. 线性表
线性表(linear list)是n个具有相同特性的数据元素的有限序列。线性表是⼀种在实际中⼴泛使⽤的数据结构,常⻅的线性表:顺序表、链表、栈、队列、字符串...线性表在逻辑上是线性结构,也就说是连续的⼀条直线。但是在物理结构上并不⼀定是连续的线性表在物理上存储时,通常以数组和链式结构的形式存储。
2. 顺序表
2.1 概念与结构
概念:顺序表是⽤⼀段物理地址连续的存储单元依次存储数据元素的线性结构,⼀般情况下采⽤数组 存储。
顺序表和数组的区别? 顺序表的底层结构是数组,对数组的封装,实现了常⽤的增删改查等接⼝
2.2 分类
2.2.1 静态顺序表
概念:使⽤定⻓数组存储元素
静态顺序表缺陷:空间给少了不够⽤,给多了造成空间浪费
2.2.2 动态顺序表
2.3 动态顺序表的实现
SeqList.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>//定义动态顺序表的结构
typedef int SLDataType;
typedef struct SeqList
{SLDataType* arr;int size; //有效数据个数int capacity; //空间容量
}SL;//typedef struct SeqList SL;void SLPrint(SL* ps);
//初始化
void SLInit(SL* ps);
//销毁
void SLDestroy(SL* ps);//尾插
void SLPushBack(SL* ps, SLDataType x);
//头插
void SLPushFront(SL* ps, SLDataType x);
//尾删
void SLPopBack(SL* ps);
//头删
void SLPopFront(SL* ps);//指定位置之前插⼊数据
void SLInsert(SL* ps, int pos, SLDataType x);
// 删除POS位置的数据
void SLErase(SL* ps, int pos);
//查找
int SLFind(SL* ps, SLDataType x);
SeqList.c
#include"SeqList.h"//初始化
void SLInit(SL* ps)
{ps->arr = NULL;ps->size = ps->capacity = 0;
}void SLCheckCapacity(SL* ps)
{if (ps->size == ps->capacity){int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;//增容//realloc第二个参数,单位是字节SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));if (tmp == NULL){perror("realloc fail!");exit(1);}ps->arr = tmp;ps->capacity = newCapacity;}
}//尾插
void SLPushBack(SL* ps, SLDataType x)
{assert(ps);//判断空间是否足够SLCheckCapacity(ps);//空间足够的情况下ps->arr[ps->size++] = x;
}//头插
void SLPushFront(SL* ps, SLDataType x)
{//温柔的处理方式//if (ps == NULL)//{// return;//}assert(ps != NULL);//判断空间是否足够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)
{//ps:限制参数不能直接给NULL//ps->size:顺序表为空assert(ps && ps->size);--ps->size;
}void SLPrint(SL* ps)
{for (int i = 0; i < ps->size; i++){printf("%d ", ps->arr[i]);}printf("\n");
}
//头删
void SLPopFront(SL* ps)
{assert(ps && 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, SLDataType x)
{assert(ps);assert(pos >= 0 && pos <= ps->size);SLCheckCapacity(ps);//pos及之后的数据整体向后挪动一位for (int i = ps->size; i > pos; i--){ps->arr[i] = ps->arr[i - 1];}ps->arr[pos] = x;++ps->size;
}
// 删除POS位置的数据
void SLErase(SL* ps, int pos)
{assert(ps);assert(pos >= 0 && pos < ps->size);//pos之后的数据整体向前挪动一位for (int i = pos; i < ps->size-1; i++){ps->arr[i] = ps->arr[i + 1];}--ps->size;
}
//查找
int SLFind(SL* ps, SLDataType x)
{for (int i = 0; i < ps->size; i++){if (ps->arr[i] == x){//找到了return i;}}//未找到return -1;
}
//销毁
void SLDestroy(SL* ps)
{assert(ps);if (ps->arr)free(ps->arr);ps->arr = NULL;ps->size = ps->capacity = 0;
}
test.c
#include"SeqList.h"void test01()
{SL sl;SLInit(&sl);SLPushBack(&sl, 1);SLPushBack(&sl, 2);SLPushBack(&sl, 3);SLPushBack(&sl, 4);SLPrint(&sl);//SLPushBack(&sl, 5);//SLPushFront(&sl, 1);//SLPushFront(&sl, 2);//SLPushFront(&sl, 3);//SLPushFront(&sl, 4);//4 3 2 1//SLPushFront(NULL, 1);////SLPopBack(&sl);//SLPrint(&sl);//SLPopBack(&sl);//SLPrint(&sl);//SLPopBack(&sl);//SLPrint(&sl);//SLPopBack(&sl);//SLPrint(&sl);//SLPopBack(&sl);
////SLPopFront(&sl);//SLPrint(&sl);//SLPopFront(&sl);//SLPrint(&sl);//SLPopFront(&sl);//SLPrint(&sl);//SLPopFront(&sl);//SLPrint(&sl);//SLPopFront(&sl);//SLPrint(&sl);
////SLInsert(&sl, 4, 100);//SLPrint(&sl);//SLErase(&sl, 3);//SLPrint(&sl);//int find = SLFind(&sl, 22222);//if (find != -1)//{// printf("找到了!\n");//}//else {// printf("未找到!\n");//}SLDestroy(&sl);
}int main()
{test01();return 0;
}