当前位置:   article > 正文

ADT - 顺序表(Squencial List)_adt写一个顺序表l

adt写一个顺序表l
仅供参考,正确性有待检验(QAQ)
  1. //ADT Squencial List
  2. #include<iostream>
  3. #define TRUE 1
  4. #define FALSE 0
  5. #define OK 1
  6. #define ERROR 0
  7. #define OVERFLOW -1
  8. #define LISTINICIALSIZE 100
  9. #define LISTINCREASE 10
  10. using namespace std;
  11. typedef int ElemType;
  12. typedef int Status;
  13. typedef struct Sq_List {
  14. ElemType *elem; //首地址
  15. int listLength; //表长
  16. int listSize; //表容量
  17. }SqList;
  18. //创建顺序表
  19. Status ListCreater_Sq(SqList &L) {
  20. L.elem = (ElemType *)malloc(sizeof(ElemType) * LISTINICIALSIZE);
  21. if (!L.elem) exit(OVERFLOW);
  22. L.listLength = 0;
  23. L.listSize = LISTINICIALSIZE;
  24. return OK;
  25. }
  26. //清空顺序表
  27. Status ListClear_Sq(SqList &L) {
  28. L.listLength = 0;
  29. return OK;
  30. }
  31. //销毁顺序表
  32. Status ListFree_Sq(SqList &L) {
  33. free(L.elem);
  34. L.listLength = L.listSize = 0;
  35. return OK;
  36. }
  37. //表容量增加
  38. void ListIncrease_Sq(SqList &L) {
  39. L.elem = (ElemType *)realloc(L.elem, sizeof(ElemType) * (L.listLength + LISTINCREASE));
  40. L.listSize += LISTINCREASE;
  41. }
  42. //线性表输入
  43. void ListInput_Sq(SqList &L) {
  44. int len;
  45. cin >> len;
  46. if (len >= L.listSize) {
  47. ListIncrease_Sq(L);
  48. }
  49. for (int i = 1; i <= len; i++) {
  50. cin >> *(L.elem + i - 1);
  51. }
  52. L.listLength = len;
  53. }
  54. //线性表插入元素
  55. Status ListInsert_Sq(SqList &L, int pos, ElemType e) {
  56. int len = L.listLength;
  57. if (pos <= 0 || pos > len + 1) return ERROR;
  58. if (L.listLength >= L.listSize) {
  59. ListIncrease_Sq(L);
  60. }
  61. for (int i = len + 1; i >= pos; i--) {
  62. *(L.elem + i) = *(L.elem + i - 1);
  63. }
  64. *(L.elem + pos - 1) = e;
  65. L.listLength++;
  66. return OK;
  67. }
  68. //线性表删除元素
  69. Status ListDelete_Sq(SqList &L, int pos, ElemType &e) {
  70. int len = L.listLength;
  71. if (pos <= 0 || pos > L.listLength) return ERROR;
  72. e = *(L.elem + pos - 1);
  73. for (int i = pos - 1; i < L.listLength; i++) {
  74. *(L.elem + i) = *(L.elem + i + 1);
  75. }
  76. L.listLength--;
  77. return OK;
  78. }
  79. //线性表改值
  80. Status ListSetVal_Sq(SqList &L, int pos, ElemType e) {
  81. if (pos > L.listLength || pos <= 0) return ERROR;
  82. *(L.elem + pos - 1) = e;
  83. return OK;
  84. }
  85. //线性表逆序
  86. void ListReverse_Sq(SqList &L) {
  87. int len = L.listLength;
  88. for (int i = 1; i <= len / 2; i++) {
  89. ElemType tmp;
  90. tmp = *(L.elem + i - 1);
  91. *(L.elem + i - 1) = *(L.elem + len - i);
  92. *(L.elem + len - i) = tmp;
  93. }
  94. }
  95. //线性表元素排序 (<)
  96. void ListSort_Sq(SqList &L) {
  97. int len = L.listLength;
  98. int j;
  99. for (int i = 2; i <= len; i++) {
  100. ElemType t = *(L.elem + i - 1);
  101. for (j = i - 1; j >= 1 && *(L.elem + j - 1) > t; j--) {
  102. *(L.elem + j) = *(L.elem + j - 1);
  103. }
  104. *(L.elem + j) = t;
  105. }
  106. }
  107. //线性表锁定元素位置
  108. int ListLocate_Sq(SqList &L, ElemType e) {
  109. int len = L.listLength;
  110. for (int i = 1; i <= len; i++) {
  111. if (*(L.elem + i - 1) == e)
  112. return i;
  113. }
  114. return 0;
  115. }
  116. //判断表空
  117. Status IsEmpty_Sq(SqList &L) {
  118. if (L.listLength == 0)
  119. return TRUE;
  120. return FALSE;
  121. }
  122. //线性表查找元素个数
  123. int ListCount_Sq(SqList &L, ElemType e) {
  124. int len = L.listLength;
  125. int cnt = 0;
  126. for (int i = 1; i <= len; i++) {
  127. if (*(L.elem + i - 1) == e)
  128. cnt++;
  129. }
  130. return cnt;
  131. }
  132. //返回表长
  133. int ListLength_Sq(SqList &L) {
  134. return L.listLength;
  135. }
  136. //返回表容量
  137. int ListSize_Sq(SqList &L) {
  138. return L.listSize;
  139. }
  140. //线性表遍历输出
  141. void ListPrint_Sq(SqList &L) {
  142. for (int i = 1; i <= L.listLength; i++) {
  143. if (i == 1)
  144. cout << *(L.elem + i - 1);
  145. else
  146. cout << ' ' << *(L.elem + i - 1);
  147. }
  148. cout << endl;
  149. }

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号