当前位置:   article > 正文

【数据结构】顺序存储线性表C语言实现_c语言有listinsert

c语言有listinsert

* 文件名:顺序存储的线性表

* 基本操作:

* InitList(&L) //构造一个空的线性表L

* DestoryList(&L) //删除一个线性表

* ClearList(&L) //将线性表重置为空表

* ListEmpty(L) //线性表的探空操作

* ListLength(L) //返回L中元素的个数

* GetElem(L, i, &e) //查找并返回第i个元素

* LocateElem(L, e, compare()) //线性表中第一个满足compare的元素

* PriorElem(L, cur_e, &pre_e) //若cur_e是线性表中元素且不是第一个,则用pre_e返回它的前驱

* NextElem(L, cur_e, &next_e) //若cur_e是线性表中元素且不是最后一个,则用pre_e返回它的后继

* ListInsert(&L, i, e) //在第i个元素之前(i-2)插入新元素e

* ListDelete(&L, i, &e) //删除L的第i个元素,并用e返回其值

* ListTraverse(L, visit()) //对每个元素用visit()进行遍历

* 下面的两个操作是书上对线性表应用的举例

* union(&La, Lb) //将Lb中不属于La的元素插入La中

* MergeList(La, Lb, &Lc) //假设La和Lb都有序,将他们归并成Lc并且也有序

* 特点:

* 顺序结构的线性表可以直接访问某个位置的元素

* 但是如果内存空间不够的话需要重新分配内存,比较麻烦

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef int ElemType; //这一行用来定义数据类型
  5. typedef int Status; //用来返回操作状态,成功返回1,失败返回0
  6. #define OK 1
  7. #define FAILED 0
  8. #define TRUE 1
  9. #define FALSE 0
  10. #define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量
  11. #define LIST_INC 10 //线性表存储空间每次增加的空间数量
  12. typedef struct {
  13. ElemType *elem; //存储空间基址
  14. int length; //当前长度(元素个数)
  15. int listsize; //当前分配的存储容量
  16. }List, *L;
  17. //构造一个新的线性表
  18. Status InitList(List &L){
  19. L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
  20. if(!L.elem){
  21. printf("malloc failed\n");
  22. return FAILED;
  23. }
  24. L.length = 0; //当前链表中没有元素
  25. L.listsize = LIST_INIT_SIZE; //链表长度赋初值
  26. return OK;
  27. }//InitList
  28. //删除一个线性表
  29. Status DestoryList(List &L){
  30. free(&L);
  31. return OK;
  32. }//DestoryList
  33. //将线性表变成一个空表
  34. //将所有单元初始化为0
  35. Status ClearList(List &L){
  36. for(int idx = 0; idx < L.length; idx++){
  37. L.elem[idx] = 0;
  38. }
  39. return OK;
  40. }
  41. //线性表的探空操作
  42. Status ListEmpty(List L){
  43. if(L.length == 0){
  44. return TRUE; //线性表是空的
  45. } else if(L.length == 1){
  46. return FALSE; //线性表不是空的
  47. }
  48. }
  49. //返回L中元素的个数
  50. int ListLength(List L){
  51. return L.length;
  52. }
  53. //返回链表中下标为i的元素
  54. ElemType GetElem(List L, int i){
  55. if(L.length < i){
  56. printf("Get error\n");
  57. return -1;
  58. }
  59. return L.elem[i];
  60. }
  61. //查找线性表中第一个和e相等的元素,并且返回其下标号
  62. //如果没有找到就返回-1
  63. int LocateElem(List L, ElemType e){
  64. if(ListEmpty(L) == TRUE){
  65. //线性表是空的
  66. printf("the list is empty\n");
  67. return -1;
  68. }
  69. for(int idx = 0; idx<L.length; idx++){
  70. if(L.elem[idx] == e){
  71. return idx;
  72. }
  73. }
  74. printf("can not find e in list\n");
  75. return -1;
  76. }
  77. //找e的前驱元素,如果存在则返回其下标
  78. //如果不存在则返回-1
  79. int PriorElem(List L, ElemType e){
  80. if(ListEmpty(L) == TRUE){
  81. printf("ths list is empty\n");
  82. return -1;
  83. }
  84. for(int idx = 0; idx<L.length; idx++){
  85. if(L.elem[idx] == e){
  86. if(idx == 0){
  87. printf("e has no prior in ths list\n");
  88. } else {
  89. return (idx-1);
  90. }
  91. }
  92. }
  93. printf("can not find prior of e\n");
  94. return -1;
  95. }
  96. //找e的后继元素
  97. int NextElem(List L, ElemType e){
  98. if(ListEmpty(L) == TRUE){
  99. printf("ths list is empty\n");
  100. return -1;
  101. }
  102. for(int idx = 0; idx<L.length; idx++){
  103. if(L.elem[idx] == e){
  104. if(idx+1 >= L.length){ //这里其实只要等于就行了
  105. printf("e has no next in ths list\n");
  106. } else {
  107. return (idx+1);
  108. }
  109. }
  110. }
  111. printf("can not find next of e\n");
  112. return -1;
  113. }
  114. //插入元素
  115. //在位置i插入元素,并且将i及i后的所有元素后移一位
  116. Status ListInsert(List &L, ElemType e, int i){
  117. if(i > L.length){ //插入位置太后面了,必须按照顺序来
  118. printf("insert error\n");
  119. return FAILED;
  120. }
  121. if(L.length >= L.listsize){ //内存空间不够,需要重新分配内存
  122. ElemType* newBase = (ElemType *)realloc(L.elem, (L.listsize+LIST_INC)*sizeof(ElemType));
  123. if(!newBase){
  124. printf("realloc failed\n");
  125. return FAILED;
  126. }
  127. L.elem = newBase;
  128. L.listsize += LIST_INC;
  129. }
  130. if(i == L.length){ //如果是插入在最后一个位置,那么直接插入就行了
  131. L.elem[i] = e;
  132. return OK;
  133. }
  134. for(int idx = L.length-1; idx>=i; idx--){
  135. L.elem[idx] = L.elem[idx+1];
  136. }
  137. L.elem[i] = e;
  138. L.length++;
  139. return OK;
  140. }
  141. //删除下标为i的元素,并用e返回
  142. //后面元素前移一格
  143. Status ListDelete(List &L, int i, ElemType &e){
  144. if(i >= L.length){
  145. printf("there is no such elem\n");
  146. return FAILED;
  147. }
  148. e = L.elem[i];
  149. for(int idx = i+1; idx<L.length; idx++){
  150. L.elem[idx] = L.elem[i-1];
  151. }
  152. L.elem[L.length-1] = 0;
  153. L.length--;
  154. return OK;
  155. }
  156. //遍历线性表中所有元素
  157. Status ListTraverse(List L){
  158. if(L.length == 0){
  159. printf("there is no elem in the list\n");
  160. return FAILED;
  161. }
  162. for(int idx = 0; idx<L.length; idx++){
  163. if(idx != 0){
  164. printf(" ");
  165. }
  166. printf("%d", L.elem[idx]);
  167. }
  168. printf("\n");
  169. return OK;
  170. }
  171. //将Lb中不属于La的元素插入La中
  172. Status ListUnion(List &La, List Lb){
  173. int La_len = ListLength(La);
  174. int Lb_len = ListLength(Lb);
  175. for(int i = 0; i < Lb_len; i++){
  176. ElemType e = GetElem(Lb, i);
  177. if(LocateElem(La, e) != -1){ //在线性表La中没有这个元素
  178. //则将e插入线性表中
  179. ListInsert(La, e, La_len);
  180. }
  181. }
  182. return OK;
  183. }
  184. //假设La和Lb都有序,将他们归并成Lc并且也有序
  185. //假设是按照递增的顺序排序的
  186. Status MergeList(List La, List Lb, List &Lc){
  187. InitList(Lc); //初始化Lc
  188. int La_len = ListLength(La);
  189. int Lb_len = ListLength(Lb);
  190. int i=0, j=0;
  191. int idx = 0; //这是Lc线性表的指针
  192. while((i<La_len) && (j<Lb_len)){ //La和Lb均非空
  193. ElemType ea = GetElem(La, i);
  194. ElemType eb = GetElem(Lb, j);
  195. if(ea < eb){
  196. ListInsert(Lc, ea, idx++);
  197. i++;
  198. } else {
  199. ListInsert(Lc, eb, idx++);
  200. j++;
  201. }
  202. }
  203. while(i<La_len){
  204. ElemType ea = GetElem(La, i);
  205. ListInsert(Lc, ea, idx++);
  206. i++;
  207. }
  208. while(j<Lb_len){
  209. ElemType eb = GetElem(Lb, j);
  210. ListInsert(Lc, eb, idx++);
  211. j++;
  212. }
  213. return OK;
  214. }

 

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

闽ICP备14008679号