当前位置:网站首页>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 .
边栏推荐
- 华为发布首款电驱动系统DriveONE:充电10分钟续航200km
- MySQL 是如何归档数据的呢?
- Chinese patent keyword extraction based on LSTM and logistic regression
- 小霸王被申请破产!公司成“老赖” ,法人被限制高消费
- 作价11.5亿元,1206件设备注入合资公司!SK海力士抢食大陆晶圆代工市场!
- The University was abandoned for three years, the senior taught himself for seven months, and found a 12K job
- Object based real-time spatial audio rendering - Dev for dev column
- 1945. sum of digits after string conversion
- 高举5G和AI两面旗帜:紫光展锐市场峰会火爆申城
- 实现瀑布流效果
猜你喜欢
![Leetcode 19. delete the penultimate node of the linked list [knowledge points: speed pointer, recursion, stack]](/img/86/c74a63c3465efbed74c2bf059bac4f.jpg)
Leetcode 19. delete the penultimate node of the linked list [knowledge points: speed pointer, recursion, stack]

Uncaught Error:Invalid geoJson format Cannot read property ‘length‘ of undefind

基于Paragraph-BERT-CRF的科技论文摘要语步功能信息识别方法研究

网格数据生成函数meshgrid

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

Edited by vimtutor

技术选型Rust——事后分析
![Leetcode interview question 02.07. Linked list intersection [knowledge points: Double pointers, stack]](/img/51/ec623bb609f5f57150e7244cf5f9b7.png)
Leetcode interview question 02.07. Linked list intersection [knowledge points: Double pointers, stack]

Kubedm builds kubernetes cluster

八、QOS队列调度与报文丢弃
随机推荐
NTP server time (view server time)
高举5G和AI两面旗帜:紫光展锐市场峰会火爆申城
详解visual studio 2015在局域网中远程调试程序
Research on the recognition method of move function information of scientific paper abstract based on paragraph Bert CRF
With the help of domestic chip manufacturers, the shipment of white brand TWS headphones has reached 600million in 2020
For the next generation chromebook, MediaTek launched new chipsets mt8192 and mt8195
MATLAB从入门到精通 第1章 MATLAB入门
Library borrowing system "suggested collection"
数据插值——对不同量级的数据进行归一化
如何优雅的设计工作流引擎(荣耀典藏版)
B+ tree height calculation of MySQL
微星宝安工厂失火!官方回应:无人员受伤,产线不受影响!
Vimtutor编辑
World Hepatitis Day | grassroots can also enjoy the three a resources. How can the smart medical system solve the "difficulty of seeing a doctor"?
How to skillfully use assertion + exception handling classes to make the code more concise! (glory Collection Edition)
Uniapp progress bar customization
不用Swagger,那我用啥?
Assign a string pointer to an array [easy to understand]
MySQL
What is the purpose of database read-write separation [easy to understand]