子任务名任务:1_2 动态顺序存储线性表的基本实现

环境:Visual Studio Code

  1. /*
  2. 子任务名任务:1_2 动态顺序存储线性表的基本实现
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #define LIST_INIT_SIZE 100
  8. #define LISTINCREMENT 10
  9. #define Status int
  10. #define OVERFLOW -1
  11. #define OK 1
  12. #define ERROR 0
  13. #define ElemType int
  14. typedef struct
  15. {
  16. ElemType * elem;
  17. int length;
  18. int listsize;//当前分配的存储容量
  19. }SqList;
  20. //函数介绍
  21. Status InitList(SqList *L); //初始化
  22. Status ListInsert(SqList *L, int i,ElemType e);//插入
  23. Status ListDelete(SqList *L,int i,ElemType *e);//删除
  24. void ListPrint(SqList L);//输出打印
  25. void DeleteMin(SqList *L);//删除最小
  26. Status InitList(SqList *L)//初始化
  27. {
  28. L->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));//申请100空间
  29. if(!L->elem)//申请失败
  30. return ERROR;
  31. L->length = 0;//长度0
  32. L->listsize = LIST_INIT_SIZE;//容量100
  33. return OK;//申请成功
  34. }
  35. Status ListInsert(SqList *L,int i,ElemType e)//插入
  36. {
  37. int j;
  38. ElemType *newbase;
  39. if(i<1 || i>L->length+1)
  40. return ERROR;//非法输入
  41. if(L->length >= L->listsize)//存满了,需要更大空间
  42. {
  43. //realloc void * 改变已分配内存的大小
  44. newbase = (ElemType*)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));//大10的空间
  45. if(!newbase)//申请失败
  46. return ERROR;
  47. L->elem = newbase;//调指针
  48. L->listsize+= LISTINCREMENT;//新容量
  49. }
  50. for(j=L->length;j>i-1;j--)//从后往前覆盖
  51. L->elem[j] = L->elem[j-1];
  52. L->elem[i-1] = e;//在留出的位置赋值
  53. L->length++;//长度+1
  54. return OK;
  55. }
  56. Status ListDelete(SqList *L,int i,ElemType *e)//删除
  57. {
  58. int j;
  59. if(i<1 || i>L->length)//非法输入/表空
  60. return ERROR;
  61. *e = L->elem[i-1];//为了返回值
  62. for(j = i-1;j <= L->length;j++)//从前往后覆盖
  63. L->elem[j] = L->elem[j+1];
  64. (L->length)--;//长度减1
  65. return OK;//返回删除值
  66. }
  67. void ListPrint(SqList L)//输出打印
  68. {
  69. int i;
  70. for(i=0;i<L.length;i++)
  71. printf("%d ",L.elem[i]);
  72. printf("\n");//为了美观
  73. }
  74. void DeleteMin(SqList *L)//删除最小
  75. {
  76. //表空在Listdelete函数里判断
  77. int i;
  78. int j=0;//最小值下标
  79. ElemType *e;
  80. for(i=0;i<L->length;i++)//寻找最小
  81. {
  82. if(L->elem[i] < L->elem[j])
  83. j=i;
  84. }
  85. /*
  86. ListDelete(L,j+1,&e) 纯C不支持*e类型,&e调用
  87. 弹出警告:
  88. warning: passing argument 3 of 'ListDelete' from incompatible pointer type [-Wincompatible-pointer-types]
  89. 警告:从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“ListDelete”的参数 3
  90. */
  91. ListDelete(L,j+1,&e);//调用删除,注意j要+1
  92. }
  93. int main(void)
  94. {
  95. SqList L;
  96. int i;
  97. ElemType e;
  98. ElemType data[9] = {11,-22,-33,3,-88,21,77,0,-9};
  99. InitList(&L);
  100. for (i = 1; i <= 9; i++)
  101. {
  102. ListInsert(&L,i,data[i-1]);
  103. }
  104. printf("插入完成后 L = : ");
  105. ListPrint(L);
  106. ListDelete(&L, 2, &e);
  107. printf("删除第 2 个后L = : ");
  108. ListPrint(L);
  109. DeleteMin(&L);
  110. printf("删除L中最小值后L = : ");
  111. ListPrint(L);
  112. DeleteMin(&L);
  113. printf("删除L中最小值后L = : ");
  114. ListPrint(L);
  115. DeleteMin(&L);
  116. printf("删除L中最小值后L = : ");
  117. ListPrint(L);
  118. }

报错

  1. ��̬˳��洢���Ա�ʾ.c: In function 'DeleteMin':
  2. ��̬˳��洢���Ա�ʾ.c:98:22: warning: passing argument 3 of 'ListDelete' from incompatible pointer type [-Wincompatible-pointer-types]
  3. ListDelete(L,j+1,&e);//调用删除,注意j要+1
  4. ^~
  5. ��̬˳��洢���Ա�ʾ.c:64:45: note: expected 'int *' but argument is of type 'int **'
  6. Status ListDelete(SqList *L,int i,ElemType *e)//删除
  7. ^

尝试原因

  1. /*
  2. ListDelete(L,j+1,&e) 纯C不支持*e类型,&e调用
  3. 弹出警告:
  4. warning: passing argument 3 of 'ListDelete' from incompatible pointer type [-Wincompatible-pointer-types]
  5. 警告:从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“ListDelete”的参数 3
  6. */
  7. ListDelete(L,j+1,&e);//调用删除,注意j要+1