本文主要介绍了C语言实现动态顺序表,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
一、各个函数接口的实现
1.1 不太好‘'李姐‘'的“容量检测函数”
对顺序表进行插入数据时,需要判断顺序表的容量是否充足,增加数据的同时需要反复地检测容量,所以推荐直接将以上步骤封装成一个函数。
函数实现算法:若容量大小 == 有效数据大小,则为现有顺序表增容一倍的空间。
但是需要注意的是:初始顺序表后,容量为0,则需开辟4个有效数据的空间。
void SeqListCheckCapacity(SLT* psl)
{
assert(psl);
if (psl->size == psl->capacity)
{
size_t newcapacity = psl->capacity == 0 ? 4 : (psl->capacity) * 2;
psl->a = (SQDatatype*)realloc(psl->a, sizeof(SQDatatype) * newcapacity);
psl->capacity = newcapacity;
}
}
1.2 在任意位置插入的函数"坑!"
算法实现:首先检测容量,再通过想要插入的下标找到位置,将包括该下标的元素以及其后的所有元素往后挪一步,最后在该下标位置放入数据。
void SeqListInsert(SLT* psl, size_t pos, SQDatatype x)
{
assert(psl);
assert(pos >= 0 && pos <= psl->size);
SeqListCheckCapacity(&psl);
int end = psl->size - 1;
while (end >= pos)
{
psl->a[end + 1] = psl->a[end];
end--;
}
psl->a[pos] = x;
psl->size++;
}
考虑到下标pos一定是个非负整数,故使用size_t类型。
如果利用该函数进行头插,即pos == 0;在while循环的最后一步,即end == pos时,end--后end变成-1,再回到while循环的判断条件时,end会出现整形提升的情况,即-1变成无符号整形,约为21亿。
end出现整形提升的原因在于pos是size_t类型。
解决方法就是保证while循环中和pos比较的式子为非负数即可。
void SeqListInsert(SLT* psl, size_t pos, SQDatatype x)
{
assert(psl);
assert(pos >= 0 && pos <= psl->size);
SeqListCheckCapacity(psl);
int end = psl->size;
while (end >= pos + 1)
{
psl->a[end] = psl->a[end - 1];
end--;
}
psl->a[pos] = x;
psl->size++;
}
1.3 在任意位置删除数据的函数
算法思路:把指定元素之后的所有元素全部向前挪动一步。
void SeqListErase(SLT* psl, size_t pos)
{
assert(psl);
assert(pos >= 0 && pos < psl->size);
size_t begin = pos;
if (begin == psl->size - 1)
{
psl->size--;
return;
}
while (begin < psl->size - 1)
{
psl->a[begin] = psl->a[begin + 1];
begin++;
}
psl->size--;
}
上述代码中if条件语句用于判断是否为尾删。
注意:避免负数与无符号数通过操作符连接,避免有符号数变成负数后被整型提升为无符号数或者强制转换。
1.4 其余简单的接口函数
初始化函数
void SeqListInit(SLT* psl)
{
assert(psl);
psl->a = NULL;
psl->capacity = psl->size = 0;
}
销毁函数
void SeqListDestory(SLT* psl)
{
assert(psl);
if (psl->a)
{
free(psl->a);
psl->a = NULL;
}
psl->capacity = psl->size = 0;
}
打印函数
void SeqListPrint(SLT* psl)
{
assert(psl);
int i = 0;
for (i = 0; i < psl->size; i++)
{
printf("%d ", psl->a[i]);
}
printf("\n");
}
尾插
void SeqListPushBack(SLT* psl, SQDatatype x)
{
assert(psl);
SeqListCheckCapacity(psl);
psl->a[psl->size] = x;
psl->size++;
}
头插
void SeqListPushFront(SLT* psl, SQDatatype x)
{
assert(psl);
SeqListCheckCapacity(psl);
int i = 0;
for (i = psl->size - 1; i >= 0; i--)
{
psl->a[i + 1] = psl->a[i];
}
psl->a[0] = x;
psl->size++;
}
尾删
void SeqListPopBack(SLT* psl)
{
assert(psl);
psl->size--;
}
头删
{
assert(psl);
assert(psl->size > 0);
int begin = 0;
while (begin < psl->size - 1)
{
psl->a[begin] = psl->a[begin + 1];
begin++;
}
psl->size--;
}
通过数据查找下标
int SeqListFind(SLT* psl, SQDatatype x)
{
assert(psl);
int begin = 0;
while (begin < psl->size)
{
if (x == psl->a[begin])
return begin;
begin++;
}
return -1;
}
二、顺序表结构体声明与定义
typedef int SQDatatype;
重定义可方便以后更换元素类型时修改
typedef struct SeqList
{
SQDatatype* a;
int size;
int capacity;
}SLT;
重定义可以让定义结构体对象(变量)时,免去代码的冗余。
如struct SeqList s1;可修改为SLT s1;
三、头文件的调用
- #include<stdio.h>标准输入输出
- #include<assert.h>断言错误,避免空指针对程序的影响
- #include<stdlib.h>动态函数
到此这篇关于新手向超详细的C语言实现动态顺序表的文章就介绍到这了,更多相关C语言动态顺序表内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
本文标题为:新手向超详细的C语言实现动态顺序表


- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30
- ubuntu下C/C++获取剩余内存 2023-09-18
- Qt计时器使用方法详解 2023-05-30
- C语言手把手带你掌握带头双向循环链表 2023-04-03
- C++ 数据结构超详细讲解顺序表 2023-03-25
- C语言详解float类型在内存中的存储方式 2023-03-27
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11
- C语言qsort()函数的使用方法详解 2023-04-26
- Easyx实现扫雷游戏 2023-02-06
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09