当前位置:网站首页>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 .
边栏推荐
- What technology is needed for applet development
- Bully is filed for bankruptcy! The company has become a "Lao Lai", and the legal person is restricted from high consumption
- Pytorch学习记录(四):过拟合、卷积神经网络CNN
- Edited by vimtutor
- 物联网技术栈之网关技术
- Adventures of little mouse: behind the scenes gags of moss 2
- 标准C语言学习总结10
- LeetCode链表问题——142.环形链表II(一题一文学会链表)
- 8、 QoS queue scheduling and message discarding
- Huawei releases the first electric drive system driveone: charging for 10 minutes, endurance of 200km
猜你喜欢

聊一聊数据库的行存与列存

Cross domain transfer learning of professional skill word extraction in Chinese recruitment documents

LeetCode链表问题——142.环形链表II(一题一文学会链表)

How to skillfully use assertion + exception handling classes to make the code more concise! (glory Collection Edition)

Several skills of API interface optimization

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

Research on intangible cultural heritage image classification based on multimodal fusion

How is nanoid faster and more secure than UUID implemented? (glory Collection Edition)

Nano gold coupled antibody / protein Kit (20nm, 1mg/100 μ g/500 μ G coupling amount) preparation

Research on weapon equipment attribute extraction based on attribute word completion
随机推荐
入行4年,跳槽2次,我摸透了软件测试这一行~
High salary in the workplace | "intermediate and advanced test" interview questions
With the help of domestic chip manufacturers, the shipment of white brand TWS headphones has reached 600million in 2020
Why on earth is it not recommended to use select *?
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
标准C语言学习总结10
PyQt5快速开发与实战 5.4 网页交互
株洲市九方中学开展防溺水、消防安全教育培训活动
Meta opens the project aria pilot dataset and will develop real-time 3D maps in the future
For the 1000 yuan 5g mobile phone market, MediaTek Tianji 700 released
C#流程控制语句
How is nanoid faster and more secure than UUID implemented? (glory Collection Edition)
Week 6 Linear Models for Classification (Part B)
[brother hero July training] day 28: dynamic planning
LeetCode链表问题——面试题02.07.链表相交(一题一文学会链表)
Kubedm builds kubernetes cluster
Research on weapon equipment attribute extraction based on attribute word completion
分而治之,大型文件分片上传
基于对象的实时空间音频渲染丨Dev for Dev 专栏
关于一些小需求,用案例方式记录