当前位置:网站首页>顺序表的基本操作
顺序表的基本操作
2022-04-23 04:07:00 【二球悬铃木丶】
今天我们来学习顺序表的基本操作。
前言
顺序表,全名顺序存储结构,是线性表的一种。
顺序表存储数据时,会提前申请一整块足够大小的物理空间,然后将数据依次存储起来,存储时做到数据元素之间不留一丝缝隙。
emm,这不就是数组吗
没错,顺序表就是计算机内存中以数组的形式保存的线性表。
一、初始化顺序表
void SLInit(SL* ps)
{
assert(ps);
ps->a = NULL;
ps->size = ps->capacity = 0;
}
二、打印顺序表
void SLPrint(SL* ps)
{
assert(ps);
for (int i = 0;i < ps->size;i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
三、销毁空间
void SLDestory(SL* ps)
{
assert(ps);
if (ps->a)
{
free(ps->a);
ps->a = NULL;
}
}
四、检查容量空间,满了扩容
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;
}
}
五、尾插
void SLPushBack(SL* ps,SLDataType x)
{
assert(ps);
//检查空间
SLCheckCapacity(ps);
ps->a[ps->size] = x;
ps->size++;
}
六、尾删
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);
ps->size--;
}
七、头插
void SLPushFront(SL* ps,SLDataType x)
{
assert(ps);
//检查空间
SLCheckCapacity(ps);
//挪动数据
int end = ps->size - 1;
while (end >= 0)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[0] = x;
ps->size++;
}
八、头删
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--;
}
九、某个位置插入
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
//检查空间
SLCheckCapacity(ps);
int end = ps->size-1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[pos] = x;
ps->size++;
}
十、某个位置删除
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--;
}
十一、查找
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;
}
十二、修改
int Modify(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
ps->a[pos] = x;
}
版权声明
本文为[二球悬铃木丶]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_63742310/article/details/124336653
边栏推荐
- Xiaomi, which has set the highest sales record of domestic mobile phones in overseas markets, paid renewed attention to the domestic market
- What if win10 doesn't have a local group policy?
- 【李宏毅2022 机器学习春】hw6_GAN(不懂..)
- TreeSet after class exercises
- Understand the gut organ axis, good gut and good health
- Machine translation baseline
- C语言:恶搞小游戏
- Why recommend you to study embedded
- Common string processing functions in C language
- ROS series (4): ROS communication mechanism series (4): topic communication practice
猜你喜欢
【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)
基于PHP的代步工具购物商城
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
小红书被曝整体裁员20%,大厂之间内卷也很严重
Difference between LabVIEW small end sequence and large end sequence
兼容NSR20F30NXT5G的小体积肖特基二极管
Xiaohongshu was exposed to layoffs of 20% as a whole, and the internal volume among large factories was also very serious
【测绘程序设计】坐标方位角推算神器(C#版)
[latex] formula group
Add the compiled and installed Mysql to the path environment variable
随机推荐
什么是软件验收测试,第三方软件检测机构进行验收测试有什么好处?
智能电子秤全国产化电子元件推荐方案
Man's life
Digital image processing third edition Gonzalez notes Chapter 2
中国移动日赚2.85亿很高?其实是5G难带来更多利润,那么钱去哪里了?
洛谷P1858 【多人背包】 (背包求前k优解)
[mapping program design] coordinate inverse artifact v1 0 (with C / C / VB source program)
STM32上μC/Shell移植与应用
[echart] démarrer avec echart
[mathematical modeling] my mathematical memory
KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
Express middleware ① (use of Middleware)
Express middleware ② (classification of Middleware)
Nel ASA:挪威Herøya设施正式启用
LabVIEW 小端序和大端序区别
What if you encounter symbols you don't know in mathematical formulas
STM32 MCU ADC rule group multi-channel conversion DMA mode
MATLAB lit plusieurs diagrammes fig et les combine en un seul diagramme (sous forme de sous - Diagramme)
小红书被曝整体裁员20%,大厂之间内卷也很严重
[string] ranking of country names ----- problem solving notes