服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 新手向超详细的C语言实现动态顺序表

新手向超详细的C语言实现动态顺序表

2022-01-10 15:13燕麦冲冲冲 C/C++

本文主要介绍了C语言实现动态顺序表,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、各个函数接口的实现

1.1 不太好‘'李姐‘'的“容量检测函数”

对顺序表进行插入数据时,需要判断顺序表的容量是否充足,增加数据的同时需要反复地检测容量,所以推荐直接将以上步骤封装成一个函数。

函数实现算法:若容量大小 == 有效数据大小,则为现有顺序表增容一倍的空间。

但是需要注意的是:初始顺序表后,容量为0,则需开辟4个有效数据的空间。

?
1
2
3
4
5
6
7
8
9
10
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 在任意位置插入的函数"坑!"

算法实现:首先检测容量,再通过想要插入的下标找到位置,将包括该下标的元素以及其后的所有元素往后挪一步,最后在该下标位置放入数据。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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比较的式子为非负数即可。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 在任意位置删除数据的函数

算法思路:把指定元素之后的所有元素全部向前挪动一步。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 其余简单的接口函数

初始化函数

?
1
2
3
4
5
6
void SeqListInit(SLT* psl)
{
 assert(psl);
 psl->a = NULL;
 psl->capacity = psl->size = 0;
}

销毁函数

?
1
2
3
4
5
6
7
8
9
10
void SeqListDestory(SLT* psl)
{
 assert(psl);
 if (psl->a)
 {
  free(psl->a);
  psl->a = NULL;
 }
 psl->capacity = psl->size = 0;
}

打印函数

?
1
2
3
4
5
6
7
8
9
10
void SeqListPrint(SLT* psl)
{
 assert(psl);
 int i = 0;
 for (i = 0; i < psl->size; i++)
 {
  printf("%d ", psl->a[i]);
 }
 printf("\n");
}

尾插

?
1
2
3
4
5
6
7
void SeqListPushBack(SLT* psl, SQDatatype x)
{
 assert(psl);
 SeqListCheckCapacity(psl);
 psl->a[psl->size] = x;
 psl->size++;
}

头插

?
1
2
3
4
5
6
7
8
9
10
11
12
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++;
}

尾删

?
1
2
3
4
5
void SeqListPopBack(SLT* psl)
{
 assert(psl);
 psl->size--;
}

头删

?
1
2
3
4
5
6
7
8
9
10
11
{
 assert(psl);
 assert(psl->size > 0);
 int begin = 0;
 while (begin < psl->size - 1)
 {
  psl->a[begin] = psl->a[begin + 1];
  begin++;
 }
 psl->size--;
}

通过数据查找下标

?
1
2
3
4
5
6
7
8
9
10
11
12
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;

重定义可方便以后更换元素类型时修改

?
1
2
3
4
5
6
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语言动态顺序表内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_54209978/article/details/120400015

延伸 · 阅读

精彩推荐