当前位置:   article > 正文

【C/C++】【数据结构】顺序表_顺序表的长度是指

顺序表的长度是指

顺序表也是线性表,具有线性结构。线性结构分为两种,一是顺序存储,在内存中是连续的;二是链式存储,在内存中是不连续的。

 

顺序表的特点:

(1) 除第一个元素外,其他所有元素都只有一个直接前驱。

(2) 除最后一个元素外,其他所有元素都只有一个直接后继。

 

顺序表有容量和长度两个属性。长度指的是,顺序表中有效元素的个数。容量指的是顺序表中最大的可能长度。

若顺序表中的有效元素为0,即长度为0,则称之为空表。

 

顺序表头文件sequencelist.h

  1. #ifndef _SEQUENCELIST_H
  2. #define _SEQUENCELIST_H
  3. #define SUCCESS 10000
  4. #define FAILURE 10001
  5. #define TRUE 10002
  6. #define FAULSE 10003
  7. #define SIZE 10
  8. typedef int ElemType;
  9. struct SequenceList
  10. {
  11. int length;
  12. ElemType *elem;
  13. };
  14. typedef struct SequenceList SqList;
  15. int ListInit(SqList *list);
  16. int ListInsert(SqList *list, int position, ElemType e);
  17. int ListLength(SqList list);
  18. int ListEmpty(SqList list);
  19. int GetElem(SqList L, int position, ElemType *elem);
  20. int ListTraverse(SqList list, void (*p)(ElemType));
  21. int LocateElem(SqList list, ElemType elem, int (*p)(ElemType, ElemType));
  22. int ListDelete(SqList *list, int position, ElemType *elem);
  23. int ListClear(SqList *list);
  24. int ListDestory(SqList *list);
  25. int ListInsertSort(SqList *list, ElemType elem, int (*p)(ElemType, ElemType));
  26. #endif

顺序表初始化

  1. int ListInit(SqList *list)
  2. {
  3. if(NULL == list)
  4. {
  5. return FAILURE;
  6. }
  7. list->length = 0;
  8. list->elem = (ElemType *)malloc( SIZE*sizeof(ElemType));
  9. if(NULL == list->elem)
  10. {
  11. return FAILURE;
  12. }
  13. return SUCCESS;
  14. }

顺序表插入

  1. int ListInsert(SqList *list, int position, ElemType elem)
  2. {
  3. int i;
  4. if(NULL == list || NULL == list->elem)
  5. {
  6. return FAILURE;
  7. }
  8. if(position > list->length + 1 || position < 1 || list->length >= SIZE)
  9. {
  10. return FAILURE;
  11. }
  12. for(i = 0; i < list->length - position +1; i++)
  13. {
  14. list->elem[list->length - i] = list->elem[list->length - i -1];
  15. }
  16. list->elem[position - 1] = elem;
  17. list->length++;
  18. return SUCCESS;
  19. }

顺序表求长

  1. int ListLength(SqList list)
  2. {
  3. return list.length;
  4. }

顺序表判空

  1. int ListEmpty(SqList list)
  2. {
  3. return (list.length == 0) ? TRUE : FAULSE;
  4. }

顺序表求值

  1. int GetElem(SqList L, int position, ElemType *elem)
  2. {
  3. if(position < 1 || position > L.length)
  4. {
  5. return FAILURE;
  6. }
  7. *elem = L.elem[position - 1];
  8. return SUCCESS;
  9. }

顺序表遍历

  1. int ListTraverse(SqList list, void (*p)(ElemType))
  2. {
  3. int i;
  4. if( 0 == list.length)
  5. {
  6. return FAILURE;
  7. }
  8. for( i = 0; i < list.length; i++)
  9. {
  10. (*p)(list.elem[i]);
  11. }
  12. return SUCCESS;
  13. }

顺序表检索

  1. int LocateElem(SqList list, ElemType elem, int (*p)(ElemType, ElemType))
  2. {
  3. int i;
  4. if( 0 == list.length)
  5. {
  6. return FAILURE;
  7. }
  8. for(i = 0; i < list.length; i++)
  9. {
  10. if(TRUE == (*p)(list.elem[i], elem))
  11. {
  12. return i+1;
  13. }
  14. }
  15. return FAILURE;
  16. }

顺序表删除

  1. int ListDelete(SqList *list, int position, ElemType *elem)
  2. {
  3. int i;
  4. if(NULL == list)
  5. {
  6. return FAILURE;
  7. }
  8. if(position > list->length + 1 || position < 1)
  9. {
  10. return FAILURE;
  11. }
  12. *elem = list->elem[position - 1];
  13. for(i = 0; i < list->length -position; i++)
  14. {
  15. list->elem[position - 1 + i] = list->elem[position + i];
  16. }
  17. list->length--;
  18. return SUCCESS;
  19. }

顺序表重置

  1. int ListClear(SqList *list)
  2. {
  3. if(NULL == list)
  4. {
  5. return FAILURE;
  6. }
  7. list->length = 0;
  8. return SUCCESS;
  9. }

顺序表销毁

  1. int ListDestory(SqList *list)
  2. {
  3. if(NULL == list)
  4. {
  5. return FAILURE;
  6. }
  7. list->length = 0;
  8. free(list->elem);
  9. list->elem = NULL;
  10. return SUCCESS;
  11. }

测试文件

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include "sequencelist.h"
  4. #include<time.h>
  5. void print(ElemType elem)
  6. {
  7. printf("%d ", elem);
  8. }
  9. int EQ(ElemType L_elem, ElemType elem)
  10. {
  11. if(L_elem == elem)
  12. {
  13. return TRUE;
  14. }
  15. else
  16. {
  17. return FALSE;
  18. }
  19. }
  20. int GT(ElemType L_elem, ElemType elem)
  21. {
  22. if(L_elem > elem)
  23. {
  24. return TRUE;
  25. }
  26. else
  27. {
  28. return FALSE;
  29. }
  30. }
  31. int LT(ElemType L_elem, ElemType elem)
  32. {
  33. if(L_elem < elem)
  34. {
  35. return TRUE;
  36. }
  37. else
  38. {
  39. return FALSE;
  40. }
  41. }
  42. int main()
  43. {
  44. SqList L;
  45. int ret, i;
  46. ElemType elem;
  47. int position = 3;
  48. srand(time(NULL));
  49. ret = ListInit(&L);
  50. if(SUCCESS == ret)
  51. {
  52. printf("Init success.\n");
  53. }
  54. else
  55. {
  56. printf("Init failure.\n");
  57. }
  58. for(i = 0; i < 12; i++)
  59. {
  60. ret = ListInsert(&L, i + 1, rand() % 10);
  61. if(SUCCESS == ret)
  62. {
  63. printf("Insert success.\n");
  64. }
  65. else
  66. {
  67. printf("Insert failure.\n");
  68. }
  69. }
  70. printf("Length = %d\n", ListLength(L));
  71. printf((ListEmpty(L) == TRUE) ? "Empty.\n" : "Not empty.\n");
  72. position = 3;
  73. ret = GetElem(L, position, &elem);
  74. if( FAILURE == ret)
  75. {
  76. printf("GetElem failure\n");
  77. }
  78. else
  79. {
  80. printf("the %dth is %d\n", position, elem);
  81. }
  82. ret = ListTraverse(L,print);
  83. if( FAILURE == ret)
  84. {
  85. printf("\nTraverse failure.\n");
  86. }
  87. else
  88. {
  89. printf("\nTraverse success.\n");
  90. }
  91. elem = 3;
  92. ret = LocateElem(L, elem, EQ);
  93. if(FAILURE == ret)
  94. {
  95. printf("failure\n");
  96. }
  97. else
  98. {
  99. printf(" %d is the %dth element\n", elem, ret);
  100. }
  101. position = 3;
  102. ret = ListDelete(&L, position, &elem);
  103. if(FAILURE == ret)
  104. {
  105. printf("Delete failure.\n");
  106. }
  107. else
  108. {
  109. printf("Delete the %dth element %d success.\n", position, elem);
  110. }
  111. ret = ListTraverse(L,print);
  112. if( FAILURE == ret)
  113. {
  114. printf("\nTraverse failure.\n");
  115. }
  116. else
  117. {
  118. printf("\nTraverse success.\n");
  119. }
  120. ret = ListClear(&L);
  121. if(FAILURE == ret)
  122. {
  123. printf("Clear failure.\n");
  124. }
  125. else
  126. {
  127. printf("Clear success.\n");
  128. }
  129. ret = ListDestory(&L);
  130. if(FAILURE == ret)
  131. {
  132. printf("Destory failure.\n");
  133. }
  134. else
  135. {
  136. printf("Destory success.\n");
  137. }
  138. for(i = 0; i < 12; i++)
  139. {
  140. ret = ListInsert(&L, i + 1, rand() % 10);
  141. if(SUCCESS == ret)
  142. {
  143. printf("Insert success.\n");
  144. }
  145. else
  146. {
  147. printf("Insert failure.\n");
  148. }
  149. }
  150. return 0;
  151. }

 

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

闽ICP备14008679号