当前位置:网站首页>顺序表(下)
顺序表(下)
2022-08-08 04:24:00 【双一周学八天】
一、顺序表的删
头删:
void SLPopFront(SL* psl)
{
assert(psl);//遇到错误直接报错
int begin = 0;
while (begin < psl->size - 1)
{
psl->a[begin] == psl->a[begin + 1];
begin++;
}
}这种方法基本用不到,一般是采用尾删,头删没采用一次就要挪动n个数据,浪费资源。
尾删:
void SLPopBack(SL* psl)
{
assert(psl);
if (psl->size == 0)
return;
psl->size--;
}二、顺序表的插入
实质上是“增删查改”的增,之所以要弄头增头减是因为那两个经常会用到,单独列出来了。
void SLInsert(SL* psl, size_t pos, SLDataType x)
{
assert(psl);
assert(pos > psl->size);
SLCheckCapacity(psl);
size_t end=psl->size-1;
while (end > pos)
{
psl->a[end] = psl->a[end - 1];
end--;
}
psl->a[pos] = x;
psl->size++;
};这里破损用到的是size_t类型,这是一个无符号整数类型,在以后的库的学习中用的还是这个,因此我们现在就应该适应。
有一些要注意的点,不可 用下面的代码实现这一功能:
int end=psl->size;
while (end >= pos)
{
psl->a[end+1] = psl->a[end];
end--;
}这样当pos=0时会导致死循环,原因如下:
end被整型提升为无符号整数,当end为-1的时候在运算的时候会直接提取内存中的:
“11111111111111111111111111111111”一个极大的正数
三、顺序表的查
如下:
int SLFind(SL* psl, SLDataType x)
{
assert(psl);
for (int i = 0; i < psl->size; i++)
{
if (psl->a[i] == x)
{
return i;
}
}
return -1;
}四、顺序表的删
void SLErase(SL* psl, size_t pos)
{
assert(psl);
assert(pos > psl->size - 1);
size_t begin = pos + 1;
while (begin <= psl->size - 1)
{
psl -> a[begin - 1] = psl->a[begin];
begin++;
}
psl->size--;
}五、顺序表的改
void SLModify(SL* psl, size_t pos, SLDataType x)
{
assert(psl);
assert(pos < psl->size);
psl->a[pos] = x;
}
边栏推荐
- 6G时代新用户面设计和关键技术
- 以0为底或以1为底对图片迭代次数的影响
- CARLA 笔记(05)— Actors and blueprints(创建和修改 Blueprint、生成 Spawning、使用 Handling、销毁 Destruction)
- Young freshmen who yearn for open source | The guide to avoiding pits from open source to employment is here!
- Risk control strategy must be learned | This method of mining rules with decision trees
- 数据库缓冲区高速缓存中块的类型
- Codeforces Round #684 (Div. 1)
- Open3D ICP精配准(使用鲁棒性核函数)
- 包 package
- KMP和EXKMP(Z函数)
猜你喜欢
随机推荐
失业在家的6个月,我通过外包全款买了房:你看不起的行业,往往很赚钱
XDR technology
Vulfocus Shooting Range Scenario Mode - Intranet Dead End
Strong Net Cup 2019 - Casual Bet (Stacked Injection)
VSCode opens some records of C (embedded) projects
unity之粒子特效制作图片拼合文字效果
Let your text be seen by more people: Come and contribute, the payment is reliable!
08 获取器 withAttr、多连缀、whereRaw、事务、数据集《ThinkPHP6 入门到电商实战》
牛客多校第6场赛后学习 B(两种做法)G(两种做法)M(两种写法)J
ToDesk企业版上新 | 十大新功能,让企业远控更安全、更便捷、更流畅
32. 你知道Redis的字符串是怎么实现的吗?
LeetCode_485_最大连续1的个数
2022/08/06 Study Notes (day24) Collection
MySQL从入门到入土【20W字收藏篇】
L3-006 Slash in the wind
Redis持久化机制、主从、哨兵、cluster集群方案解析
reduce具体使用以及使用reduce,toString,flat进行数组降维
sessionStorage在不同页签中的数据是否共享问题及解决思路
高薪程序员&面试题精讲系列134之微服务网关有哪些限流算法?如何实现限流?
MySQL——索引与事务









