当前位置:网站首页>顺序表的基本操作
顺序表的基本操作
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
边栏推荐
- Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
- C语言常用字符串处理函数
- /etc/bash_completion.d目录作用(用户登录立刻执行该目录下脚本)
- 【NeurIPS 2019】Self-Supervised Deep Learning on Point Clouds by Reconstructing Space
- [latex] formula group
- Operating skills of spot gold_ Wave estimation curve
- 知乎有问题,谁来解答?
- Overview of knowledge map (II)
- 为什么推荐你学嵌入式
- 秒杀所有区间相关问题
猜你喜欢
![[AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022](/img/b2/269ae2e9be269c2bff73eb1da5b55d.png)
[AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022

知乎有问题,谁来解答?
![[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中间件②(中间件的分类)

Matlab reads multiple fig graphs and then combines them into one graph (in the form of sub graph)

单片机串口数据处理(2)——uCOSIII+循环队列接收数据

智能电子秤全国产化电子元件推荐方案

LabVIEW 小端序和大端序区别

C语言:恶搞小游戏

减治思想——二分查找详细总结
随机推荐
ROS series (4): ROS communication mechanism series (4): topic communication practice
Retrieval question answering system baseline
[mathematical modeling] my mathematical memory
Cuda11 is installed perfectly in win10 X + pytorch 1.9 (blood flowing into the river) cuda. is_ Available() becomes true!
Does China Mobile earn 285 million a day? In fact, 5g is difficult to bring more profits, so where is the money?
OpenCV----YOLACT实例分割模型推理
Win10 boot VMware virtual machine boot seconds blue screen problem perfect solution
无线充电全国产化电子元件推荐方案
VHDL implementation of 32-bit binary to BCD code
matlab讀取多張fig圖然後合並為一張圖(子圖的形式)
[mapping program design] coordinate azimuth calculation artifact (version C)
Xiaohongshu was exposed to layoffs of 20% as a whole, and the internal volume among large factories was also very serious
Qtspim manual - Chinese Translation
【李宏毅2022 机器学习春】hw6_GAN(不懂..)
Shopping mall for transportation tools based on PHP
Nel ASA: her ø Ya facility in Norway officially opened
Mysql出现2013 Lost connection to MySQL server during query
ERROR: Could not find a version that satisfies the requirement win32gui
Second kill all interval related problems
[Li Hongyi 2022 machine learning spring] hw6_ Gan (don't understand...)