当前位置:网站首页>Implementation of sequence table
Implementation of sequence table
2022-07-28 21:52:00 【Science52】
1. The linear table
The linear table (linear list) yes n A finite sequence of data elements with the same characteristics .
Linear table is a kind of widely used in practice Data structure used , Common linear tables : The order sheet 、 Linked list 、 Stack 、 queue 、 character string ... A linear table is logically a linear structure , That is to say, it is a continuous straight line . But it is not necessarily continuous in physical structure , When a linear table is physically stored , It is usually stored in the form of array and chain structure .
2. The order sheet
2.1 Concept and structure
A sequence table is a linear structure in which data elements are sequentially stored in a section of storage cells with continuous physical addresses , Generally, array storage is used Store . Add, delete, check and modify the data on the array . The order table can be divided into :
1. Static sequence table : Use a fixed length array to store elements .
2. Dynamic sequence table : Use dynamic array storage .
2.2 Interface implementation
The static sequence table is only applicable to the scenario where you know how much data needs to be stored . A fixed length array of a static order table results in N It must be big , empty There is too much waste in the room , It's not enough . Therefore, in reality, dynamic sequence tables are basically used , Dynamically allocate space as needed size , So let's implement the dynamic sequence table .

The first thing we need to do is initialize and destroy the sequence table , This step is best done in succession , Because this can prevent yourself from forgetting to release
void SLInit(Seqlist* psl)
{
psl->a =NULL;
psl->capacity = 0;
psl->sz = 0;
}
void SLdestory(Seqlist* psl)
{
if (psl->a != NULL)
{
free(psl->a);
psl->a = NULL;
psl->capacity = 0;
psl->sz = 0;
}
}We have to check the capacity increase before inserting , So we can write a function independently , Let each function run independently
void checkcapacity(Seqlist* psl)// Check the capacity increase
{
assert(psl);
if (psl->sz == psl->capacity)
{
int newcapacity = psl->capacity == 0 ? 4 : 2 * psl->capacity;
SLDatatype* tmp = (SLDatatype*)realloc(psl->a, newcapacity * sizeof(SLDatatype));
if (tmp == NULL)
{
perror("realloc fail");// Prevent expansion failure
return;
}
else
{
psl->a = tmp;
psl->capacity = newcapacity;
}
}
}Below is our head plug deletion , The tail is inserted and deleted
void SLpophead(Seqlist* psl)// Head deletion
{
assert(psl);
checkcapacity(psl);
int begin = 0;
while (begin < psl->sz - 1)
{
psl->a[begin] = psl->a[begin + 1];
++begin;
}
psl->sz--;
}
void SLpushhead(Seqlist* psl, SLDatatype x)// Head insertion
{
assert(psl);
checkcapacity(psl);
int end = psl->sz-1;
while (end--)
{
psl->a[end+1] = psl->a[end];
}
psl->a[0] = x;
psl->sz++;
}
void SLpushback(Seqlist* psl, SLDatatype x)// Tail insertion
{
assert(psl);
checkcapacity(psl);
psl->a[psl->sz] = x;
psl->sz++;
}
void SLpopback(Seqlist* psl)// Deletion at the end
{
assert(psl);
assert(psl->sz > 0);
psl->sz--;
}After that, it can be printed out for easy observation , Then there is the function of printing
void SLPrint(Seqlist* psl)
{
int i = 0;
for (i = 0; i < psl->sz; i++)
{
printf("%d ", psl->a[i]);
}
}Simple plug and tail deletion obviously does not meet our requirements when processing data
Now let's delete and insert anywhere
void SLInsert(Seqlist* psl,size_t pos, SLDatatype x)// Insert at any position
{
assert(psl);
assert(pos <= psl->sz);
checkcapacity(psl);
size_t end = psl->sz ;
while (end >pos)
{
psl->a[end] = psl->a[end-1];
end--;
}
psl->a[pos] = x;
psl->sz++;
}
void SLErase(Seqlist* psl, size_t pos)// Delete anywhere
{
assert(psl);
assert(pos < psl->sz);
size_t begin = pos;
while (begin < psl->sz-1)
{
psl->a[begin] = psl->a[begin + 1];
++begin;
}
psl->sz--;
}The code of deleting the tail of the plug can be modified , Just call anywhere to delete , The inserted function can be modified ,
So what's the significance of making a head and tail deletion function alone ?
In fact, this is also meaningful , Because when dealing with data, we often choose head plug deletion and tail insertion deletion . So it's better to encapsulate a function independently .
边栏推荐
- 作价11.5亿元,1206件设备注入合资公司!SK海力士抢食大陆晶圆代工市场!
- Why does Baidu search only crawl, but not show the page?
- 百度搜索符合预期,但涉及外链黑帽策略,什么原因?
- 世界肝炎日 | 基层也能享受三甲资源,智慧医疗系统如何解决“看病难”?
- Week 6 Linear Models for Classification (Part B)
- Versailles ceiling: "the monthly salary of two years after graduation is only 35K, which is really unpromising ~ ~"
- 【英雄哥七月集训】第 28天:动态规划
- Leetcode linked list question - interview question 02.07. linked list intersection (learn linked list by one question and one article)
- 物联网技术栈之网关技术
- Automatic filling of spare parts at mobile end
猜你喜欢
![[英雄星球七月集训LeetCode解题日报] 第28日 动态规划](/img/79/bc763bb6f12c525454abda18be4265.png)
[英雄星球七月集训LeetCode解题日报] 第28日 动态规划

The University was abandoned for three years, the senior taught himself for seven months, and found a 12K job

Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
![Leetcode 142. circular linked list II [knowledge points: speed pointer, hash table]](/img/74/321a4a0fab0b0dbae53b2ea1faf814.png)
Leetcode 142. circular linked list II [knowledge points: speed pointer, hash table]

【Bluetooth蓝牙开发】八、BLE协议之传输层

LT7911D Type-C/DP转mipi 方案成熟可提供技术支持

Log slimming operation: how to optimize from 5g to 1g! (glory Collection Edition)

基于BRNN的政务APP评论端到端方面级情感分析方法

基于复杂网络的大群体应急决策专家意见与信任信息融合方法及应用

Knowledge description framework of foreign patent documents based on knowledge elements
随机推荐
[Bluetooth Bluetooth development] VIII. Transmission layer of ble protocol
LeetCode链表问题——面试题02.07.链表相交(一题一文学会链表)
Achieve waterfall effect
将字符串指针赋值给数组[通俗易懂]
Leetcode linked list problem -- 142. circular linked list II (learn the linked list by one question and one article)
蚂蚁集团境外站点 Seata 实践与探索
比UUID更快更安全NanoID到底是怎么实现的?(荣耀典藏版)
The ref value ‘xxx‘ will likely have changed by the time this effect function runs. If this ref......
[英雄星球七月集训LeetCode解题日报] 第28日 动态规划
Log slimming operation: how to optimize from 5g to 1g! (glory Collection Edition)
Cloud security core technology
标准C语言学习总结10
大学荒废三年,大四自学7个月测试,找到了12K的工作
Standard C language learning summary 10
MySQL
基于Paragraph-BERT-CRF的科技论文摘要语步功能信息识别方法研究
不用Swagger,那我用啥?
C语言入门【详细】
Layout the 6G track in advance! Ziguang zhanrui released the white paper "6G unbounded AI"
For the next generation chromebook, MediaTek launched new chipsets mt8192 and mt8195