当前位置:网站首页>Basic operation of sequence table
Basic operation of sequence table
2022-04-23 04:37:00 【Two ball Sycamore】
Today, let's learn the basic operation of sequence table .
List of articles
- Preface
- One 、 Initialization sequence table
- Two 、 Print order table
- 3、 ... and 、 Destroy space
- Four 、 Check capacity space , Full capacity expansion
- 5、 ... and 、 Tail insertion
- 6、 ... and 、 Deletion at the end
- 7、 ... and 、 Head insertion
- 8、 ... and 、 Head deletion
- Nine 、 Insert... At a certain location
- Ten 、 Delete a location
- 11、 ... and 、 lookup
- Twelve 、 modify
Preface
The order sheet , Full name sequential storage structure , It's a kind of linear table .
When a sequence table stores data , Will apply for a whole piece of physical space of sufficient size in advance , And then store the data one by one , When storing, there is no gap between data elements .
emm, Isn't this an array
you 're right , The sequence table is A linear table stored as an array in computer memory .
One 、 Initialization sequence table
void SLInit(SL* ps)
{
assert(ps);
ps->a = NULL;
ps->size = ps->capacity = 0;
}
Two 、 Print order table
void SLPrint(SL* ps)
{
assert(ps);
for (int i = 0;i < ps->size;i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
3、 ... and 、 Destroy space
void SLDestory(SL* ps)
{
assert(ps);
if (ps->a)
{
free(ps->a);
ps->a = NULL;
}
}
Four 、 Check capacity space , Full capacity expansion
void SLCheckCapacity(SL* ps)
{
assert(ps);
if (ps->capacity == ps->size)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapacity * sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc");
exit(-1);
}
ps->a = tmp;
ps->capacity = newCapacity;
}
}
5、 ... and 、 Tail insertion
void SLPushBack(SL* ps,SLDataType x)
{
assert(ps);
// Check the space
SLCheckCapacity(ps);
ps->a[ps->size] = x;
ps->size++;
}
6、 ... and 、 Deletion at the end
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);
ps->size--;
}
7、 ... and 、 Head insertion
void SLPushFront(SL* ps,SLDataType x)
{
assert(ps);
// Check the space
SLCheckCapacity(ps);
// Move data
int end = ps->size - 1;
while (end >= 0)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[0] = x;
ps->size++;
}
8、 ... and 、 Head deletion
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size>0);
int begin = 1;
while (begin < ps->size)
{
ps->a[begin-1] = ps->a[begin];
++begin;
}
ps->size--;
}
Nine 、 Insert... At a certain location
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
// Check the space
SLCheckCapacity(ps);
int end = ps->size-1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[pos] = x;
ps->size++;
}
Ten 、 Delete a location
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
int begin = pos;
while (begin < ps->size-1)
{
ps->a[begin] = ps->a[begin + 1];
++begin;
}
ps->size--;
}
11、 ... and 、 lookup
int SLFind(SL* ps, SLDataType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (x == ps->a[i])
return i;
}
return -1;
}
Twelve 、 modify
int Modify(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
ps->a[pos] = x;
}
版权声明
本文为[Two ball Sycamore]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230407343803.html
边栏推荐
- Bacterial infection and antibiotic use
- Record your own dataset with d435i, run orbslam2 and build a dense point cloud
- Single chip microcomputer serial port data processing (1) -- serial port interrupt sending data
- Migrate from MySQL database to AWS dynamodb
- 【论文阅读】【3d目标检测】Improving 3D Object Detection with Channel-wise Transformer
- Interaction of diet gut microbiota on cardiovascular disease
- IEEE Transactions on Industrial Informatics(TII)投稿须知
- 上海航芯技术分享 | ACM32 MCU安全特性概述
- QML进阶(四)-绘制自定义控件
- test
猜你喜欢
![[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022](/img/eb/916a3fc1a89356fd8e74b943172ac3.png)
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022

Express中间件①(中间件的使用)

test

为什么推荐你学嵌入式

Express middleware ① (use of Middleware)

MYSQL50道基础练习题
![[BIM introduction practice] wall hierarchy and FAQ in Revit](/img/95/e599c7547029f57ce23ef4b87e8b9a.jpg)
[BIM introduction practice] wall hierarchy and FAQ in Revit

Common string processing functions in C language
![[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022](/img/48/0e95841743bada4faf3edfee31cb6a.png)
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022

指纹Key全国产化电子元件推荐方案
随机推荐
【Echart】echart 入門
用D435i录制自己的数据集运行ORBslam2并构建稠密点云
STM32 MCU ADC rule group multi-channel conversion DMA mode
shell wc (统计字符数量)的基本使用
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
C语言: 指针的进阶
Thought of reducing Governance -- detailed summary of binary search
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
C语言:恶搞小游戏
Unipolar NRZ code, bipolar NRZ code, 2ASK, 2FSK, 2PSK, 2DPSK and MATLAB simulation
Set classic topics
mysql ,binlog 日志查询
[mapping program design] coordinate azimuth calculation artifact (version C)
Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
Go反射法则
电钻、电锤、电镐的区别
IEEE Transactions on Industrial Informatics(TII)投稿须知
[AI vision · quick review of robot papers today, issue 31] Fri, 15 APR 2022
Jetpack 之 LifeCycle 组件使用详解