当前位置:网站首页>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
边栏推荐
- [echart] Introduction to echart
- Installation of zynq platform cross compiler
- [AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022
- [echart] démarrer avec echart
- 【论文阅读】【3d目标检测】Improving 3D Object Detection with Channel-wise Transformer
- STM32上μC/Shell移植与应用
- 补充番外14:cmake实践项目笔记(未完待续4/22)
- A lifetime of needs, team collaboration can play this way on cloud nailing applet
- [AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
- IDE idea automatic compilation and configuration of on update action and on frame deactivation
猜你喜欢
383. Ransom letter
MYSQL去重方法汇总
无线充电全国产化电子元件推荐方案
基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦
智能电子秤全国产化电子元件推荐方案
Go reflection rule
Supplément: annotation
Interaction of diet gut microbiota on cardiovascular disease
Bacterial infection and antibiotic use
Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)
随机推荐
The perfect combination of collaborative process and multi process
SQL statement for adding columns in MySQL table
Coinbase:关于跨链桥的基础知识、事实和统计数据
顺序表的基本操作
2021数学建模国赛一等奖经验总结与分享
Huawei machine test -- high precision integer addition
2020 is coming to an end, special and unforgettable.
Coinbase: basic knowledge, facts and statistics about cross chain bridge
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
Mysql, binlog log query
【论文阅读】【3d目标检测】Improving 3D Object Detection with Channel-wise Transformer
MySQL 2013 lost connection to MySQL server during query
KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
C language character constant
Express middleware ① (use of Middleware)
Supplement: Annotation
从MySQL数据库迁移到AWS DynamoDB
Redis 命令大全
【BIM+GIS】ArcGIS Pro2. 8 how to open Revit model, Bim and GIS integration?
【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection