顺序表其它操作

顺序表插入元素

向已有顺序表中插入数据元素,根据插入位置的不同,可分为以下 3 种情况:

  1. 插入到顺序表的表头;
  2. 在表的中间位置插入元素;
  3. 尾随顺序表中已有元素,作为顺序表中的最后一个元素;


虽然数据元素插入顺序表中的位置有所不同,但是都使用的是同一种方式去解决,即:通过遍历,找到数据元素要插入的位置,然后做如下两步工作:

  • 将要插入位置元素以及后续的元素整体向后移动一个位置;
  • 将元素放到腾出来的位置上;


例如,在 {1,2,3,4,5} 的第 3 个位置上插入元素 6,实现过程如下:

  • 遍历至顺序表存储第 3 个数据元素的位置,如图 1 所示:

    找到目标元素位置
    图 1 找到目标元素位置
  • 将元素 3 以及后续元素 4 和 5 整体向后移动一个位置,如图 2 所示:

    将插入位置腾出
    图 2 将插入位置腾出
  • 将新元素 6 放入腾出的位置,如图 3 所示:

    插入目标元素
    图 3 插入目标元素


因此,顺序表插入数据元素的 C 语言实现代码如下:

  1. //插入函数,其中,elem为插入的元素,add为插入到顺序表的位置
  2. table addTable(table t,int elem,int add)
  3. {
  4. //判断插入本身是否存在问题(如果插入元素位置比整张表的长度+1还大(如果相等,是尾随的情况),或者插入的位置本身不存在,程序作为提示并自动退出)
  5. if (add>t.length+1||add<1) {
  6. printf("插入位置有问题 ");
  7. return t;
  8. }
  9. //做插入操作时,首先需要看顺序表是否有多余的存储空间提供给插入的元素,如果没有,需要申请
  10. if (t.length==t.size) {
  11. t.head=(int *)realloc(t.head, (t.size+1)*sizeof(int));
  12. if (!t.head) {
  13. printf("存储分配失败 ");
  14. return t;
  15. }
  16. t.size+=1;
  17. }
  18. //插入操作,需要将从插入位置开始的后续元素,逐个后移
  19. for (int i=t.length-1; i>=add-1; i--) {
  20. t.head[i+1]=t.head[i];
  21. }
  22. //后移完成后,直接将所需插入元素,添加到顺序表的相应位置
  23. t.head[add-1]=elem;
  24. //由于添加了元素,所以长度+1
  25. t.length++;
  26. return t;
  27. }

注意,动态数组额外申请更多物理空间使用的是 realloc 函数。并且,在实现后续元素整体后移的过程,目标位置其实是有数据的,还是 3,只是下一步新插入元素时会把旧元素直接覆盖。

顺序表删除元素

从顺序表中删除指定元素,实现起来非常简单,只需找到目标元素,并将其后续所有元素整体前移 1 个位置即可。

后续元素整体前移一个位置,会直接将目标元素删除,可间接实现删除元素的目的。

例如,从 {1,2,3,4,5} 中删除元素 3 的过程如图 4 所示:



图 4 顺序表删除元素的过程示意图


因此,顺序表删除元素的 C 语言实现代码为:

  1. table delTable(table t,int add){
  2. if (add>t.length || add<1) {
  3. printf("被删除元素的位置有误 ");
  4. return t;
  5. }
  6. //删除操作
  7. for (int i=add; i<t.length; i++) {
  8. t.head[i-1]=t.head[i];
  9. }
  10. t.length--;
  11. return t;
  12. }

顺序表查找元素

顺序表中查找目标元素,可以使用多种查找算法实现,比如说二分查找算法、插值查找算法等。

这里,我们选择顺序查找算法,具体实现代码为:

  1. //查找函数,其中,elem表示要查找的数据元素的值
  2. int selectTable(table t,int elem){
  3. for (int i=0; i<t.length; i++) {
  4. if (t.head[i]==elem) {
  5. return i+1;
  6. }
  7. }
  8. return -1;//如果查找失败,返回-1
  9. }

顺序表更改元素

顺序表更改元素的实现过程是:

  1. 找到目标元素;
  2. 直接修改该元素的值;


顺序表更改元素的 C 语言实现代码为:

  1. //更改函数,其中,elem为要更改的元素,newElem为新的数据元素
  2. table amendTable(table t,int elem,int newElem){
  3. int add=selectTable(t, elem);
  4. t.head[add-1]=newElem;//由于返回的是元素在顺序表中的位置,所以-1就是该元素在数组中的下标
  5. return t;
  6. }


以上是顺序表使用过程中最常用的基本操作,这里给出本节完整的实现代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define Size 5
  4. typedef struct Table{
  5. int * head;
  6. int length;
  7. int size;
  8. }table;
  9. table initTable(){
  10. table t;
  11. t.head=(int*)malloc(Size*sizeof(int));
  12. if (!t.head)
  13. {
  14. printf("初始化失败 ");
  15. exit(0);
  16. }
  17. t.length=0;
  18. t.size=Size;
  19. return t;
  20. }
  21. table addTable(table t,int elem,int add)
  22. {
  23. if (add>t.length+1||add<1) {
  24. printf("插入位置有问题 ");
  25. return t;
  26. }
  27. if (t.length>=t.size) {
  28. t.head=(int *)realloc(t.head, (t.size+1)*sizeof(int));
  29. if (!t.head) {
  30. printf("存储分配失败 ");
  31. }
  32. t.size+=1;
  33. }
  34. for (int i=t.length-1; i>=add-1; i--) {
  35. t.head[i+1]=t.head[i];
  36. }
  37. t.head[add-1]=elem;
  38. t.length++;
  39. return t;
  40. }
  41. table delTable(table t,int add){
  42. if (add>t.length || add<1) {
  43. printf("被删除元素的位置有误 ");
  44. return t;
  45. }
  46. for (int i=add; i<t.length; i++) {
  47. t.head[i-1]=t.head[i];
  48. }
  49. t.length--;
  50. return t;
  51. }
  52. int selectTable(table t,int elem){
  53. for (int i=0; i<t.length; i++) {
  54. if (t.head[i]==elem) {
  55. return i+1;
  56. }
  57. }
  58. return -1;
  59. }
  60. table amendTable(table t,int elem,int newElem){
  61. int add=selectTable(t, elem);
  62. t.head[add-1]=newElem;
  63. return t;
  64. }
  65. void displayTable(table t){
  66. for (int i=0;i<t.length;i++) {
  67. printf("%d ",t.head[i]);
  68. }
  69. printf(" ");
  70. }
  71. int main(){
  72. table t1=initTable();
  73. for (int i=1; i<=Size; i++) {
  74. t1.head[i-1]=i;
  75. t1.length++;
  76. }
  77. printf("原顺序表: ");
  78. displayTable(t1);
  79. printf("删除元素1: ");
  80. t1=delTable(t1, 1);
  81. displayTable(t1);
  82. printf("在第2的位置插入元素5: ");
  83. t1=addTable(t1, 5, 2);
  84. displayTable(t1);
  85. printf("查找元素3的位置: ");
  86. int add=selectTable(t1, 3);
  87. printf("%d ",add);
  88. printf("将元素3改为6: ");
  89. t1=amendTable(t1, 3, 6);
  90. displayTable(t1);
  91. return 0;
  92. }

程序运行结果为:

原顺序表:
1 2 3 4 5
删除元素1:
2 3 4 5
在第2的位置插入元素5:
2 5 3 4 5
查找元素3的位置:
3
将元素3改为6:
2 5 6 4 5

原文地址:https://www.cnblogs.com/pudonglin/p/13215032.html