当前位置:   article > 正文

C语言实现顺序表(增,删,改,查)_c语言查表实现

c语言查表实现

目录

一.概念:

1.静态顺序表:使用定长数组存储元素。

2.动态顺序表:使用动态开辟的数组存储。

二.顺序表的实现:

1.顺序表增加元素

1.检查顺序表

2.头插

3.尾插

2.顺序表删除元素

1.头删

2.尾删

3.指定位置删

3.顺序表查找元素

4.顺序表修改元素

1.指定位置修改:

顺序表的问题:


数据结构和算法概述-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/lh11223326/article/details/136221673

一.概念:

顺序表在数据结构中是线性表的一种。

顺序表是用一段物理地址连续的存储单元依次存储数据的线性结构,一般情况下采用数组存储,在数组上完成数据的增删查改......................................

顺序表可以分为:

1.静态顺序表:使用定长数组存储元素.................

#include<stdio.h> 
#define N 7
 typedef int SLDataType;
 typedef struct SeqList
 {
    SLDataType array[N];//定长数组
    size_t size;//有效数据的个数
 }SeqList;

2.动态顺序表:使用动态开辟的数组存储。

#include<stdio.h>
 typedef struct SeqList{//顺序表的动态存储
    SLDataType*array;//指向动态开辟的数组
    size_t size;//有效数据个数
    size_t capicity;//容量空间的大小
 }SeqList;

二.顺序表的实现:

一般来说我们写大型程序的时候会把声明跟引入文件放在一个头文件中如下,创建一个SeqList.h文件把下列代码放入:

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #pragma once
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<assert.h>
  6. //动态顺序表
  7. typedef int SLDataType;
  8. typedef struct SeqList {
  9. SLDataType* a;//去堆上动态开辟,用来指向动态开辟的数组
  10. int size;//存储的有效数据个数
  11. int capacity;//空间大小
  12. }SL;
  13. //对顺序表进行管理:增删查改
  14. void SLInit(SL *ps);//顺序表初始化
  15. void SLDestroy(SL *ps);//顺序表销毁
  16. void SLPrint(SL* ps);
  17. void SLCheckCapacity(SL* ps);
  18. //头插头删,尾插尾删
  19. void SLPushBack(SL* ps, SLDataType x);//后插
  20. void SLPopBack(SL* ps);//后删
  21. void SLPushFront(SL* ps, SLDataType x);//头插
  22. void SLPopFront(SL* ps);//头删
  23. //返回下标,没有找到返回-1
  24. //找数据
  25. int SLFind(SL* ps, SLDataType x);
  26. //在指定位置插入x
  27. void SLInsert(SL* ps,int pos,SLDataType x );
  28. //删除指定位置的值
  29. void SLErase(SL* ps, int pos);
  30. //修改坐标处的元素
  31. void SLModify(SL* ps, int pos, SLDataType x);

1.顺序表增加元素

在增加元素之前需要初始化顺序表,此时需要使用malloc创建一块空间,代码如下:

  1. void SLInit(SL *ps) {//顺序表的初始化
  2. assert(ps);
  3. ps->a = (SLDataType*)malloc(sizeof(SLDataType)*4);
  4. if (ps->a==NULL) {
  5. perror("malloc failed");
  6. exit(-1);//终止程序
  7. }
  8. ps->size = 0;
  9. ps->capacity = 4;
  10. }

1.检查顺序表

如果顺序表满了就需要扩容........................

  1. void SLCheckCapacity(SL* ps) {
  2. assert(ps);
  3. //满了要扩容
  4. if (ps->size == ps->capacity) {
  5. SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDataType)));
  6. if (tmp == NULL) {
  7. perror("realloc failed");
  8. exit(-1);
  9. }
  10. ps->a = tmp;
  11. ps->capacity *= 2;
  12. }
  13. }

2.头插

在头部插入数据之前需要把全部数据都往后移动一位............

  1. void SLPushFront(SL* ps, SLDataType x) {
  2. assert(ps);
  3. SLCheckCapacity(ps);
  4. //挪动数据
  5. int end = ps->size - 1;
  6. while (end >= 0) {
  7. ps->a[end + 1] = ps->a[end];
  8. --end;
  9. }
  10. ps->a[0] = x;
  11. ps->size++;
  12. }

3.尾插

在末尾插入数据之前检查一下,如果有数据就是满了先扩容再插入......

  1. void SLPushBack(SL* ps, SLDataType x) {
  2. assert(ps);
  3. SLCheckCapacity(ps);
  4. ps->a[ps->size] = x;
  5. ps->size++;
  6. }

2.顺序表删除元素

1.头删

  1. void SLPopFront(SL* ps) {
  2. assert(ps);
  3. assert(ps->size > 0);
  4. int begin = 1;
  5. while (begin < ps->size) {
  6. ps->a[begin - 1] = ps->a[begin];
  7. ++begin;
  8. }
  9. ps->size--;
  10. }

2.尾删

直接把有效数据个数减一就行了...........

  1. void SLPopBack(SL* ps) {
  2. assert(ps);
  3. assert(ps->size > 0);
  4. ps->size--;
  5. }

3.指定位置删

把数据删除之后把数据都移前面补全.........

  1. void SLErase(SL* ps, int pos) {
  2. assert(ps);
  3. assert(pos >= 0 && pos < ps->size);
  4. int begin = pos + 1;
  5. while (begin < ps->size) {
  6. ps->a[begin - 1] = ps->a[begin];
  7. ++begin;
  8. }
  9. ps->size--;
  10. }

3.顺序表查找元素

把每个数据都比对一遍然后返回下标.........

  1. int SLFind(SL* ps, SLDataType x) {
  2. assert(ps);
  3. for (int i = 0; i < ps->size; i++) {
  4. if (ps->a[i] == x) {
  5. return i;
  6. }
  7. }
  8. return -1;
  9. }

4.顺序表修改元素

代码的位置是用户指定下标位置的修改:

  1. void SLModify(SL* ps, int pos, SLDataType x) {
  2. assert(ps);
  3. assert(pos >= 0 && pos < ps->size);
  4. ps->a[pos] = x;
  5. }

1.指定位置修改:

从指定位置的最后面把每个数据都后移一位,不要从前面往后面移,要从后往后,移动完数据之后就可以在指定位置添加了......

  1. void SLInsert(SL* ps, int pos, SLDataType x) {
  2. assert(ps);
  3. assert(pos >= 0 && pos <= ps->size);
  4. SLCheckCapacity(ps);
  5. int end = ps->size - 1;
  6. while (end>=pos) {
  7. ps->a[end + 1] = ps->a[end];
  8. --end;
  9. }
  10. ps->a[pos] = x;
  11. ps->size++;
  12. }

如下是SeqList.c中全部实现函数代码:

  1. #include"SeqList.h"
  2. void SLInit(SL *ps) {//顺序表的初始化
  3. assert(ps);
  4. ps->a = (SLDataType*)malloc(sizeof(SLDataType)*4);
  5. if (ps->a==NULL) {
  6. perror("malloc failed");
  7. exit(-1);//终止程序
  8. }
  9. ps->size = 0;
  10. ps->capacity = 4;
  11. }
  12. void SLDestroy(SL *ps)//顺序表的销毁
  13. {
  14. assert(ps);
  15. free(ps->a);
  16. ps->a = NULL;
  17. ps->capacity = ps->size = 0;
  18. }
  19. void SLPrint(SL* ps) {
  20. assert(ps);
  21. for (int i = 0; i < ps->size; i++) {
  22. printf("%d ", ps->a[i]);
  23. }
  24. printf("\n");
  25. }
  26. void SLCheckCapacity(SL* ps) {
  27. assert(ps);
  28. //满了要扩容
  29. if (ps->size == ps->capacity) {
  30. SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDataType)));
  31. if (tmp == NULL) {
  32. perror("realloc failed");
  33. exit(-1);
  34. }
  35. ps->a = tmp;
  36. ps->capacity *= 2;
  37. }
  38. }
  39. void SLPushBack(SL* ps, SLDataType x) {
  40. assert(ps);
  41. /*SLCheckCapacity(ps);
  42. ps->a[ps->size] = x;
  43. ps->size++;*/
  44. SLInsert(ps, ps->size, x);
  45. }
  46. void SLPopBack(SL* ps) {//
  47. assert(ps);
  48. //直接报错 暴力检查
  49. assert(ps->size > 0);
  50. ps->size--;
  51. //就是无效删除退出 检查
  52. /*if (ps->size == 0) {
  53. return;
  54. }*/
  55. }
  56. void SLPushFront(SL* ps, SLDataType x) {
  57. assert(ps);
  58. SLInsert(ps, 0, x);
  59. }
  60. void SLPopFront(SL* ps) {
  61. assert(ps);
  62. SLErase(ps, ps->size-1);
  63. }
  64. int SLFind(SL* ps, SLDataType x) {
  65. assert(ps);
  66. for (int i = 0; i < ps->size; i++) {
  67. if (ps->a[i] == x) {
  68. return i;
  69. }
  70. }
  71. return -1;
  72. }
  73. //在指定位置插入x
  74. void SLInsert(SL* ps, int pos, SLDataType x) {
  75. assert(ps);
  76. assert(pos >= 0 && pos <= ps->size);
  77. SLCheckCapacity(ps);
  78. int end = ps->size - 1;
  79. while (end>=pos) {
  80. ps->a[end + 1] = ps->a[end];
  81. --end;
  82. }
  83. ps->a[pos] = x;
  84. ps->size++;
  85. }
  86. //删除指定位置的值
  87. void SLErase(SL* ps, int pos) {
  88. assert(ps);
  89. assert(pos >= 0 && pos < ps->size);
  90. int begin = pos + 1;
  91. while (begin < ps->size) {
  92. ps->a[begin - 1] = ps->a[begin];
  93. ++begin;
  94. }
  95. ps->size--;
  96. }
  97. void SLModify(SL* ps, int pos, SLDataType x) {
  98. assert(ps);
  99. assert(pos >= 0 && pos < ps->size);
  100. ps->a[pos] = x;
  101. }

最后可以自行设计一个界面:

顺序表的问题:

  1. 中间/头部的插入删除,时间复杂度为O(N).
  2. 增容需要申请新空间,拷贝数据,释放旧空间,会有不小的消耗。
  3. 增容一般是呈2倍的增长,势必会有一定的空间浪费,如容量为100,满了以后增容到200,再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。

C语言单链表-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/lh11223326/article/details/137055900?spm=1001.2014.3001.5501

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/1004130
推荐阅读