【数据结构】线性表之顺序存储结构

前端之家收集整理的这篇文章主要介绍了【数据结构】线性表之顺序存储结构前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码如下:

公共头文件common.h

  1. #ifndef __HI_COMM_H__
  2. #define __HI_COMM_H__
  3.  
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <malloc.h>
  8. #include <string>
  9.  
  10. #define LIST_INIT_SIZE 100 /*线性表存储空间的初始分配量;*/
  11. #define LIST_INCREMENT 10 /*线性表存储空间的分配增量;*/
  12. #define ElemType int
  13. //#define NULL (void*)0
  14.  
  15. /*线性表的顺序存储结构*/
  16. typedef struct{
  17. ElemType *elem; //线性表的基地址
  18. int length; //当前长度
  19. int MaxSize; //当前分配的存储容量
  20. }sqlist;
  21.  
  22. /*线性表的链式存储结构*/
  23. typedef struct frankLNode
  24. {
  25. ElemType data;
  26. struct frankLNode* next;
  27. }LNode; //
  28.  
  29.  
  30. /*栈的顺序存储结构*/
  31. typedef struct
  32. {
  33. ElemType* base;
  34. ElemType* top;
  35. int StackSize;
  36. }FStack;
  37. #define STACK_INIT_SIZE 100 /*栈存储空间的初始分配量;*/
  38. #define STACK_INCREMENT 10 /*栈存储空间的分配增量;*/
  39.  
  40.  
  41. /*队列的顺序存储结构*/
  42. typedef struct frankQNode
  43. {
  44. ElemType data;
  45. struct frankQNode *next;
  46. }QNode;
  47.  
  48. typedef struct frankQueueHeader
  49. {
  50. QNode* front;
  51. QNode* rear;
  52. }QueueHeader;
  53.  
  54. /*二叉树的存储结构*/
  55.  
  56. typedef struct frankBinTreeNode
  57. {
  58. ElemType data;
  59. struct frankBinTreeNode *left;
  60. struct frankBinTreeNode *right;
  61. }BinTreeNode;
  62. typedef BinTreeNode* pBinTreeNode;
  63.  
  64.  
  65. #endif
数据结构算法的实现

文件LinearList.h

  1. #pragma once
  2. #include "common.h"
  3.  
  4. class LinearList
  5. {
  6. public:
  7. LinearList(void);
  8. ~LinearList(void);
  9.  
  10.  
  11.  
  12. int InitList(sqlist *L);
  13.  
  14. int DestroyList(sqlist *L);
  15.  
  16. int ClearList(sqlist *L);
  17.  
  18. bool isListEmpty(sqlist *L);
  19.  
  20. int getListLength(sqlist *L);
  21.  
  22. //给定一个序号,去查找这个序号对应的元素值;
  23. ElemType getElem(sqlist *L,int index);
  24.  
  25. //从列表中找到某一元素的位置
  26. int locateElem(sqlist *L,ElemType Q);
  27.  
  28. ElemType PriorElem(sqlist *L,ElemType Cur_e);
  29.  
  30. ElemType NextElem(sqlist *L,ElemType Cur_e);
  31.  
  32. int AgainMalloc(sqlist *L);
  33.  
  34. sqlist* ListInsert(sqlist *L,int index,ElemType e);
  35.  
  36. void ListDelete(sqlist *L,ElemType &e);
  37.  
  38. sqlist* ChangeElem(sqlist *L,ElemType e);
  39.  
  40. void Printsqlist(sqlist *L);
  41.  
  42. sqlist* ListAdd(sqlist *L,ElemType e);
  43.  
  44. void MergeList(sqlist La,sqlist Lb,sqlist &Lc);
  45. };
数据结构算法实现:

NodeList.cpp

  1. #include "LinearList.h"
  2.  
  3. LinearList::LinearList(void)
  4. {
  5. }
  6.  
  7. LinearList::~LinearList(void)
  8. {
  9. }
  10.  
  11. int LinearList::InitList(sqlist *L)
  12. {
  13. L->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
  14. if (!L->elem)
  15. {
  16. return -1;
  17. }
  18. L->length = 0;
  19. L->MaxSize = LIST_INIT_SIZE;
  20. return 0;
  21. }
  22.  
  23. int LinearList::DestroyList(sqlist *L)
  24. {
  25. if (L == NULL)
  26. {
  27. exit(1);
  28. }
  29.  
  30. free(L);
  31. L = (sqlist *)0;
  32. return 0;
  33. }
  34.  
  35. int LinearList::ClearList(sqlist *L)
  36. {
  37. if (L == NULL)
  38. {
  39. exit(1);
  40. }
  41. free(L->elem);
  42. L->elem = (ElemType*)0;
  43. L->MaxSize = 0;
  44. L->length = 0;
  45. return 0;
  46. }
  47.  
  48. bool LinearList::isListEmpty(sqlist *L)
  49. {
  50. if (L == NULL)
  51. {
  52. exit(1);
  53. }
  54. if (L->MaxSize == 0)
  55. {
  56. return true;
  57. }else
  58. {
  59. return false;
  60. }
  61. }
  62.  
  63. int LinearList::getListLength(sqlist *L)
  64. {
  65. if (L == NULL)
  66. {
  67. exit(1);
  68. }
  69.  
  70. return L->length;
  71. }
  72.  
  73. //给定一个序号,去查找这个序号对应的元素值;
  74. ElemType LinearList::getElem(sqlist *L,int index)
  75. {
  76. if (L == NULL)
  77. {
  78. exit(1);
  79. }
  80. else if (index<0 || index>L->length)
  81. {
  82. printf("wrong index number in Function getElem!\n");
  83.  
  84. }
  85. return L->elem[index];
  86. }
  87.  
  88. //从列表中找到某一元素的位置
  89. int LinearList::locateElem(sqlist *L,ElemType e)
  90. {
  91. if (L == NULL)
  92. {
  93. exit(1);
  94. }
  95.  
  96. for (int i = 0;i<L->length;i++)
  97. {
  98. if (L->elem[i] == e)
  99. {
  100. return i+1;
  101. }
  102. }
  103. return -1;
  104. }
  105.  
  106. ElemType LinearList::PriorElem(sqlist *L,ElemType Cur_e)
  107. {
  108. if (L == NULL)
  109. {
  110. exit(1);
  111. }
  112. for (int i = 1 ;i<L->length; i++)
  113. {
  114. if (L->elem[i] == Cur_e)
  115. {
  116. return L->elem[i-1];
  117. }
  118. }
  119. exit(1);
  120. }
  121.  
  122. ElemType LinearList::NextElem(sqlist *L,ElemType Cur_e)
  123. {
  124. if (L == NULL)
  125. {
  126. exit(1);
  127. }
  128. for (int i = 0 ;i<L->length-1; i++)
  129. {
  130. if (L->elem[i] == Cur_e)
  131. {
  132. return L->elem[i+1];
  133. }
  134. }
  135. exit(1);
  136. }
  137.  
  138. int LinearList::AgainMalloc(sqlist *L)
  139. {
  140. ElemType* p = (ElemType*)malloc((L->MaxSize + LIST_INCREMENT ) * sizeof(ElemType));
  141. if (!p)
  142. {
  143. printf("Malloc Again Fail\n");
  144. exit(1);
  145. }
  146. memcpy(p,L->elem,sizeof(L->elem)*L->MaxSize); //之前遗漏这一句,注意,要把以前的元素拷贝过来
  147. L->elem = p;
  148. L->MaxSize = L->MaxSize + LIST_INCREMENT;
  149. return 0;
  150. }
  151.  
  152. sqlist* LinearList::ListInsert(sqlist *L,ElemType e)
  153. {
  154. if (L == NULL)
  155. {
  156. exit(1);
  157. }
  158. if (index<0||index>L->length)
  159. {
  160. printf("index error in function ListInsert\n");
  161. exit(1);
  162. }
  163.  
  164. if (L->length == L->MaxSize)
  165. {
  166. //重新分配更大的空间
  167. AgainMalloc(L);
  168. }
  169. for (int i = L->length+1; i>index; i--)
  170. {
  171. L->elem[i]=L->elem[i-1];
  172. }
  173. L->elem[index] = e;
  174. L->length = L->length + 1;
  175. return L;
  176. }
  177.  
  178. sqlist* LinearList::ListAdd(sqlist *L,ElemType e)
  179. {
  180. if (L == NULL)
  181. {
  182. exit(1);
  183. }
  184.  
  185. if (L->length == L->MaxSize)
  186. {
  187. //重新分配更大的空间
  188. AgainMalloc(L);
  189. }
  190. L->elem[L->length]=e;
  191. L->length = L->length + 1;
  192. return L;
  193. }
  194.  
  195. void LinearList::ListDelete(sqlist *L,ElemType &e)
  196. {
  197. if (L == NULL)
  198. {
  199. exit(1);
  200. }
  201. if (index<0 || index>=L->length)
  202. {
  203. printf("index error in function ListDelete\n");
  204. exit(1);
  205. }
  206.  
  207. for (int i = index; i< L->length-1; i++)
  208. {
  209. L->elem[i] = L->elem[i+1];
  210. }
  211. L->length = L->length - 1;
  212. e = L->elem[index];
  213. }
  214.  
  215. sqlist* LinearList::ChangeElem(sqlist *L,ElemType e)
  216. {
  217. if (L == NULL)
  218. {
  219. exit(1);
  220. }
  221. if (index<0 || index>=L->length)
  222. {
  223. printf("index error in function ListDelete\n");
  224. exit(1);
  225. }
  226. L->elem[index] = e;
  227. return L;
  228.  
  229. }
  230.  
  231. void LinearList::Printsqlist(sqlist *L)
  232. {
  233. if (L == NULL)
  234. {
  235. exit(1);
  236. }
  237.  
  238. for (int i = 0; i< L->length; i++)
  239. {
  240. printf("sqlist[%d]:%d ",i,getElem(L,i));
  241. }
  242. printf("\n");
  243. }
  244.  
  245. //归并;
  246. void LinearList::MergeList(sqlist La,sqlist &Lc)
  247. {
  248. Lc.elem = (ElemType *)malloc((La.length + Lb.length) * sizeof(ElemType));
  249. if (!Lc.elem)
  250. {
  251. exit(1);
  252. }
  253.  
  254. ElemType* pa = La.elem;
  255. ElemType* pb = Lb.elem;
  256. ElemType* pc = Lc.elem;
  257. ElemType* pa_last = La.elem + La.length - 1;
  258. ElemType* pb_last = Lb.elem + Lb.length - 1;
  259.  
  260. while (pa<=pa_last && pb<=pb_last)
  261. {
  262. if (*pa <= *pb)
  263. {
  264. *pc++ = *pa++;
  265. }
  266. else
  267. {
  268. *pc++ = *pb++;
  269. }
  270.  
  271. }
  272.  
  273. while (pa <= pa_last)
  274. {
  275. *pc++ = *pa++;
  276. }
  277. while(pb <= pb_last)
  278. {
  279. *pc++ = *pb++;
  280. }
  281. }


测试:
  1. void TEST_LinearList()
  2. {
  3. printf("-----------------------------------------------------\n");
  4. printf("-------Here is A Test For LinearList-----------------\n");
  5.  
  6. sqlist* L = (sqlist *)malloc(sizeof(sqlist));
  7. LinearList linearLIST;
  8. linearLIST.InitList(L);
  9. int i;
  10. printf("Add Elem!\n");
  11. for (i = 0;i<10;i++)
  12. {
  13. linearLIST.ListAdd(L,i);
  14. }
  15. linearLIST.Printsqlist(L);
  16.  
  17. linearLIST.isListEmpty(L);
  18.  
  19. int k = linearLIST.locateElem(L,5);
  20.  
  21. sqlist* Q = linearLIST.ListInsert(L,5,123);
  22. linearLIST.Printsqlist(L);
  23. linearLIST.Printsqlist(Q);
  24.  
  25. ElemType e;
  26. linearLIST.ListDelete(L,8,e);
  27. linearLIST.Printsqlist(L);
  28. }


说明:代码仅供参考,有错误的还望指正。

猜你在找的数据结构相关文章