当前位置:网站首页>Implementation of a sequence table
Implementation of a sequence table
2022-07-31 03:17:00 【blue cat knight】
目录
Construction of basic functions
3.尾插(Insert data from the tail)
序言
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储.在数组上完成数据的增删查改
The implementation of the sequence table can generally be divided into two types:静态的顺序表(It is stored in the form of a fixed-length array) 和 动态的顺序表(使用动态开辟的数组进行存储)
静态顺序表只适用于确定知道需要存多少数据的场景.静态顺序表的定长数组导致N定大了,空
间开多了浪费,开少了不够用.所以现实中基本都是使用动态顺序表,根据需要动态的分配空间
大小,所以下面我们实现动态顺序表.
About the frame structure:
The basic function of the sequence table 增 删 查 找 改 插入 几大功能,We do this with functions 下面 我们 先来 对 Structure is constructed, One more declaration of the base function,This completes the basic framework.
typedef struct SeqList
{
int* a;
int size; // 存储数据的个数
int capicity; // Because we have a dynamic sequence table here,We need a capacity.
}SL;
// 初始化
void SeqListInit(SL* psl);
// 检查空间,如果满了,进行增容
void CheckCapacity(SL* psl);
// 顺序表尾插
void SeqListPushBack(SL* psl, int x);
// 顺序表尾删
void SeqListPopBack(SL* psl);
// 顺序表头插
void SeqListPushFront(SL* psl, int x);
// 顺序表头删
void SeqListPopFront(SL* psl);
// 顺序表查找
int SeqListFind(SL* psl, int x);
// 顺序表在pos位置插入x
void SeqListInsert(SL* psl, size_t pos, int x);
// 顺序表删除pos位置的值
void SeqListErase(SL* psl, size_t pos);
// 顺序表销毁
void SeqListDestory(SL* psl);
// 顺序表打印
void SeqListPrint(SL* psl);
Construction of basic functions
1.初始化数据
将 a 指针置为空 以及把size 和 capicity 的大小置为 0 ;
void SeqListInit(SL* psl)
{
assert(psl);
psl->a = NULL;
psl->size = psl->capicity = 0;
}
2.销毁数据
void SeqListDestory(SL* psl);
void SeqListDestory(SL* psl)
{
if (psl->a)
{
free(psl->a);
psl->a = NULL;
psl->capicity = psl->size = 0;
}
}
3.尾插(Insert data from the tail)
Because we have a dynamic sequence table here Think about it when inserting data 我们的capicity容量是否充足?
Here we are going to use a function;void CheckCapacity(SL* psl)
Determine whether there is enough memory area for inserting data 不够的话 We will do dynamic expansion;
检查扩容
void CheckCapacity(SL* psl)
void CheckCapacity(SL* psl)
{
assert(psl);
if (psl->capicity == psl->size)
{
int newcapicity = psl->size == 0 ? 4 : (psl->size) * 2;
int* tmp = (int*)realloc(psl->a,sizeof(int)*newcapicity);
if (tmp == NULL)
{
perror("ralloc fail");
return;
}
psl->a = tmp;
psl->capicity = newcapicity;
}
}
Now let's construct the tail plug again:
void SeqListPushBack(SL* psl, int x)
{
assert(psl);
CheckCapacity(psl);
psl->a[psl->size] = x;
psl->size++;
}
4.尾删
这个比较简单,We explained earlier in the struct size is used to count data;It is based on when we print the data size 来进行打印的.Now since the issue of printing data is mentioned So let's write a function that prints data first
打印数据
void SeqListPrint(SL* psl);
void SeqListPrint(SL* psl)
{
assert(psl);
int i = 0;
for (i = 0; i < psl->size; i++)
{
printf("%d ", psl->a[i]);
}
printf("\n");
}
尾删
直接把 size to reduce 就行了;因为我们是根据sizeThe size of the data to determine the storage.
void SeqListPopBack(SL* psl)
{
assert(psl);
if (psl->size == 0)
{
return;
}
psl->size--;
}
5.头插
在 Insert a data at the beginning; 同样是插入 同样也需要 检查扩容;Actually here's the head plug 效率不高 Also traverse the mobile data;
void SeqListPushFront(SL* psl, int x)
{
assert(psl);
CheckCapacity(psl);
int i = 0;
for (i = psl->size+1; i > 0; i--)
{
psl->a[i] = psl->a[i-1];
}
psl->a[0] = x;
psl->size++;
}
6.头删
void SeqListPopFront(SL* psl)
{
assert(psl);
assert(psl->size > 0);
int i = 0;
for (i = 0; i < psl->size-1;i++)
{
psl->a[i] = psl->a[i + 1];
}
psl->size--;
}
7.顺序表查找
Find the subscript where a data is located 返回值为 int 如果不存在 则返回 -1.
int SeqListFind(SL* psl, int x)
{
assert(psl);
int i = 0;
for (i = 0; i < psl->size; i++)
{
if (psl->a[i] == x)
return i;
}
return -1;
}
8.顺序表在pos位置插入x
Insert to cite 检查扩容
void SeqListInsert(SL* psl, size_t pos, int x)
{
assert(psl);
CheckCapacity(psl);
size_t i = 0;
for (i = psl->size + 1; i > pos; i--)
{
psl->a[i] = psl->a[i - 1];
}
psl->a[pos] = x;
psl->size++;
}
9.顺序表删除pos位置的值
利用 SeqListFind 来找出 pos 位置的下标 利用for遍历 数组 从 This subscript position 依次向前覆盖;
void SeqListErase(SL* psl, size_t pos)
{
assert(psl);
int A = SeqListFind(psl, pos);
int i = 0;
for (i = A; i < psl->size-1; i++)
{
psl->a[A] = psl->a[A + 1];
}
psl->size--;
}
改进和优化
我们知道 data in our data sheet Not necessarily all plastic In order to facilitate us to modify the data;
We can use it in advance #define 来 定义 类型的名称;
以上代码 The assertion part is not sufficient,需要进行优化.
边栏推荐
- 品牌广告投放平台的中台化应用与实践
- Mysql 45讲学习笔记(二十四)MYSQL主从一致
- Several common errors when using MP
- The distance value between two arrays of LeetCode simple questions
- 【动态规划】连续子数组的最大和
- 【CocosCreator 3.5】CocosCreator get network status
- 浅识Flutter 基本组件之showDatePicker方法
- Ambiguous method call.both
- False positives and false negatives in testing are equally worthy of repeated corrections
- VS QT - ui does not display newly added members (controls) || code is silent
猜你喜欢
[Compilation principle] Lexical analysis program design principle and implementation
什么是分布式锁?实现分布式锁的三种方式
STM32问题合集
LeetCode中等题之分数加减运算
The simulation application of common mode inductance is here, full of dry goods for everyone
[Compilation principle] Design principle and implementation of recursive descent parsing
Ambiguous method call.both
Project (5) - Small target detection tph-yolov5
浅识Flutter 基本组件之showDatePicker方法
识Flutter 基本组件之showTimePicker 方法
随机推荐
【HCIP】ISIS
What is SQALE
Crypto Firms Offer Offer To Theft Hackers: Keep A Little, Give The Rest
YOLOV5 study notes (2) - environment installation + operation + training
品牌广告投放平台的中台化应用与实践
els 方块向左移动条件判断
12 Disk related commands
Select the smoke test case, and make the first pass for the product package entering QA
SQL 面试用题(重点)
刚出道“一战成名”,安全、舒适一个不落
测试中的误报和漏报同样的值得反复修正
MultipartFile文件上传
JS function this context runtime syntax parentheses array IIFE timer delay self.backup context call apply
Graphical lower_bound & upper_bound
Mysql 45讲学习笔记(二十五)MYSQL保证高可用
Day32 LeetCode
[C language] Preprocessing operation
Multilingual settings of php website (IP address distinguishes domestic and foreign)
Detailed explanation of TCP and UDP
Number 16, top posts