线性表的定义:N个数据元素的有限序列

线性表从存储结构上分为:顺序存储结构(数组)和 链式存储结构(链表)

顺序存储结构:是用一段连续的内存空间存储表中的数据 L=(a1,a2,a3....an)

链式存储结构:是用一段一段连续的内存空间存储表中每一行的数据,段与段之间通过一个引用(指针)相互连接来,形成一个链式的存储结构

看到顺序存储结构的图示,我们可能会马上联想到C语言的数组。是的,数组就是一种典型的顺序存储数据结构。下面我通过一个实例,来实现对顺序存储结构中的数据增、删、改、查的操作。

首先定一个描述线性表数据的顺序存储结构:

  1. // 默认增长因子
  2. #define DEFAULT_CAPACITY 10
  3.  
  4. typedef unsigned int * PU32;
  5.  
  6. typedef unsigned int U32;
  7.  
  8. /************************************************************************/
  9. /* 线性表数据结构,存储数组元素、数组大小、数组增长因子,下面简称为动态数组 */
  10. /************************************************************************/
  11. typedef struct _tag_ArrayList
  12. {
  13. PU32 container; // 存储数组元素的容器
  14. int length; // 数组长度
  15. int capacity; // 数组增长因子
  16. } TArrayList;

container:是一个无符号32位的指针数组,用于存储线性表中的所有元素,后面的增、删、改查都是操作这个操作中指针元素

length:记录数组中的元数数量,表示这个线性表中存储了多少个数据元素

capacity:因为要实现数组动态扩容的功能,这个值代表数组满后,每次扩容的大小,默认为10

线性表初始化

  1. ArrayList * ArrayList_CreateDefault()
  2. {
  3. return ArrayList_Create(DEFAULT_CAPACITY);
  4. }
  5.  
  6. ArrayList * ArrayList_Create(int capacity)
  7. {
  8. if (capacity < 0)
  9. {
  10. printf("fun ArrayList_Create error, Illegal capacity: %d", capacity);
  11. return NULL;
  12. }
  13.  
  14. TArrayList *list = (TArrayList *)malloc(sizeof(TArrayList));
  15. if (list == NULL)
  16. {
  17. printf("out of memory, create ArrayList fail!");
  18. return NULL;
  19. }
  20.  
  21. list->capacity = capacity;
  22. list->length = 0;
  23. list->container = (PU32)malloc(sizeof(PU32)* DEFAULT_CAPACITY);
  24. if (list->container == NULL)
  25. {
  26. printf("out of memory, create Array container fail!");
  27. return NULL;
  28. }
  29. memset(list->container, 0, sizeof(PU32)* DEFAULT_CAPACITY);
  30.  
  31. return list;
  32. }

ArrayList_CreateDefault:初始化默认10个元素大小的线性表存储空间

ArrayList_Create:根据capacity大小初始化

其中ArrayList是通过typedef void定义的一个类型,因为想屏蔽内部实现线性表的数据存储结构,使用者无须关注里面的实现细节,只需保存这个线性表句柄的引用,当每次操作线性表时,都将这个句柄作为形参传给相应的函数即可。

往线性表中指定位置添加元素

  1. int ArrayList_AddItem(ArrayList *list, ArrayItem *item, int pos)
  2. {
  3. int ret = 0;
  4. TArrayList *arrayList = NULL;
  5. if (list == NULL || item == NULL)
  6. {
  7. ret = -1;
  8. printf("fun ArrayList_AddItem error:%d of the list is NULL or item is NULL\n", ret);
  9. return ret;
  10. }
  11.  
  12. arrayList = (TArrayList *)list;
  13.  
  14. // 容错处理,如果当前存储了3个元素,要在第5个位置插入一个元素,将插入位置改成第4个位置
  15. // |0|1|2|3| | |
  16. if (pos > arrayList->length)
  17. {
  18. pos = arrayList->length;
  19. }
  20.  
  21. // 扩容
  22. PU32 newContainer = NULL;
  23. if (arrayList->length > 0 && arrayList->length % arrayList->capacity == 0)
  24. {
  25. newContainer = (PU32)malloc(sizeof(PU32)* (arrayList->length + arrayList->capacity));
  26. if (newContainer == NULL)
  27. {
  28. ret = -2;
  29. printf("out of memory, add item fail!");
  30. return ret;
  31. }
  32. memset(newContainer, 0, arrayList->length * sizeof(PU32));
  33.  
  34. // 将旧的数据拷贝到新的容器中
  35. memcpy(newContainer, arrayList->container, arrayList->length * sizeof(PU32));
  36.  
  37. // 释放旧容器的内存
  38. free(arrayList->container);
  39. // 指向新容器的地址
  40. arrayList->container = newContainer;
  41. }
  42.  
  43. // 将元素插入到数组pos位置
  44. for (int i = arrayList->length; i > pos; i--)
  45. {
  46. arrayList->container[i] = arrayList->container[i - 1];
  47. }
  48.  
  49. arrayList->container[pos] = (U32)item;
  50. arrayList->length++;
  51.  
  52. return ret;
  53. }

1、添加前,首先判断再添加一个元素后,数组是否会满,如果会满就先扩容

arrayList->length > 0 && arrayList->length % arrayList->capacity == 0

2、将元素插入到数组中指定的位置。如果当前数组中有5个元素,新元素要插入到数组的第3个位置,所以第3个位置和后面的元素都要往后移

for (int i = arrayList->length; i > pos; i--)

{

arrayList->container[i] = arrayList->container[i - 1];

}

arrayList->container[pos] = (U32)item;

arrayList->length++; // 长度+1



删除线性表中指定位置的元素(与添加相反)

  1. ArrayItem * ArrayList_RemoveItem(ArrayList *list, int pos)
  2. {
  3. TArrayList *arrayList = NULL;
  4. ArrayItem *arrayItem = NULL;
  5. arrayList = checkRange(list, pos);
  6. if (arrayList == NULL)
  7. {
  8. return NULL;
  9. }
  10.  
  11. // 缓存删除的元素
  12. arrayItem = (ArrayItem *)arrayList->container[pos];
  13.  
  14. // 从数组中移徐指定索引的元素
  15. for (int i = pos; i < arrayList->length; i++)
  16. {
  17. arrayList->container[i] = arrayList->container[i + 1];
  18. }
  19. arrayList->length--; // 长度减1
  20.  
  21. return arrayItem; // 返回被删除的元素
  22. }

删除前首先保存要删除的元素,删除成功返回将该元素返回

还有其它操作(在表尾添加元素、获取、遍历、清除、销毁等),这里不一一讲解了,源代码中有详细的注释,下面给出完整的源代码与测试用例:

  1. //
  2. // ArrayList.h
  3. // 线性表存储--动态数组
  4. //
  5. // Created by 杨信 on 14-5-15.
  6. // Copyright (c) 2014年 yangxin. All rights reserved.
  7. //
  8.  
  9. #ifndef __ArrayList_H__
  10. #define __ArrayList_H__
  11.  
  12. #ifdef _cplusplus
  13. extern "C" {
  14. #endif
  15.  
  16. typedef void ArrayList; // 线性表句柄
  17. typedef void ArrayItem; // 线性表的条目
  18.  
  19. /*
  20. 遍历数组的回调函数
  21. @item : 当前元素
  22. @pos : 当前元素的索引
  23. */
  24. typedef void(*_Each_Function)(ArrayItem *item, int pos);
  25.  
  26. /*
  27. 创建一个默认capacity大小的动态数组
  28. capacity默认为10
  29. @return 动态数组句柄
  30. */
  31. ArrayList * ArrayList_CreateDefault();
  32.  
  33. /*
  34. 创建一个初始化容量为capacity大小的动态数组
  35. @capacity : 数组大小增长因子,如果数组已满,则自动增长capacity大小
  36. @return 动态数组句柄
  37. */
  38. ArrayList * ArrayList_Create(int capacity);
  39.  
  40. /*
  41. 在指定位置添加(插入)一个元素
  42. @list : 动态数组句柄
  43. @item : 新添加的数组元素
  44. @pos : 插入位置(索引从0开始)
  45. @return 插入成功返回0,失败返回非0值
  46. */
  47. int ArrayList_AddItem(ArrayList *list, ArrayItem *item, int pos);
  48.  
  49. /*
  50. 在数组某尾添加一个元素
  51. @list : 动态数组句柄
  52. @item : 新添加的数组元素
  53. @return 插入成功返回0,失败返回非0值
  54. */
  55. int ArrayList_AddItemBack(ArrayList *list, ArrayItem *item);
  56.  
  57. /*
  58. 删除一个数组元素
  59. @list : 动态数组句柄
  60. @pos : 插入位置(索引从0开始)
  61. @return 删除成功返回被删除的元素,失败返回NULL
  62. */
  63. ArrayItem * ArrayList_RemoveItem(ArrayList *list, int pos);
  64.  
  65. /*
  66. 清空数组所有元素
  67. @list : 动态数组句柄
  68. @return 成功返回0,失败返回非0值
  69. */
  70. int ArrayList_Clear(ArrayList *list);
  71.  
  72. /*
  73. 修改数组元素
  74. @list : 动态数组句柄
  75. @item : 新元素
  76. @pos : 修改元素的索引
  77. @return 修改成功返回0,失败返回非0值
  78. */
  79. int ArrayList_SetItem(ArrayList *list, ArrayItem *item, int pos);
  80.  
  81. /*
  82. 获取数组指定位置的元素
  83. @list : 动态数组句柄
  84. @pos : 元素索引
  85. @return 返回数组指定位置的元素,如果pos大于等于数组长度或小于0,则返回NULL
  86. */
  87. ArrayItem * ArrayList_GetItem(ArrayList *list, int pos);
  88.  
  89. /*
  90. 遍历数组
  91. @list : 动态数组句柄
  92. @_fun_callback : 遍历数组中每个元素时的回调函数
  93. 函数原型:void(*_Each_Function)(ArrayItem *item, int pos);
  94. 示例:void ArrayEachCallback(ArrayItem *item, int pos) { ... }
  95. */
  96. void ArrayList_For_Each(ArrayList *list, _Each_Function _fun_callback);
  97.  
  98. /*
  99. 遍历数组指定范围内的元素
  100. @list : 动态数组句柄
  101. @begin : 元素开始位置
  102. @end : 元素结束位置
  103. @_fun_callback : 遍历数组中每个元素时的回调函数
  104. 函数原型:void(*_Each_Function)(ArrayItem *item, int pos);
  105. 示例:void ArrayEachCallback(ArrayItem *item, int pos) { ... }
  106. */
  107. void ArrayList_For_Each_Range(ArrayList *list, int begin, int end,
  108. _Each_Function _fun_callback);
  109.  
  110. /*
  111. 获取动态数组的长度(存储的元素数量)
  112. @list : 动态数组句柄
  113. @return 数组长度
  114. */
  115. int ArrayList_GetLength(ArrayList * list);
  116.  
  117. /*
  118. 获取动态数组的增长因子
  119. @list : 动态数组句柄
  120. @return 数组增长因子
  121. */
  122. int ArrayList_GetCapacity(ArrayList * list);
  123.  
  124. /*
  125. 销毁动态数组(释放内存)
  126. @list : 动态数组句柄
  127. @return 销毁成功返回0,失败返回非0值
  128. */
  129. int ArrayList_Destory(ArrayList **list);
  130.  
  131. #ifdef _cplusplus
  132. }
  133. #endif
  134.  
  135. #endif
  136.  
  137. //
  138. // ArrayList.c
  139. // 线性表存储--动态数组
  140. //
  141. // Created by 杨信 on 14-5-15.
  142. // Copyright (c) 2014年 yangxin. All rights reserved.
  143. //
  144.  
  145. #include <stdio.h>
  146. #include <stdlib.h>
  147. #include <string.h>
  148. #include "ArrayList.h"
  149.  
  150. // 默认增长因子
  151. #define DEFAULT_CAPACITY 10
  152.  
  153. typedef unsigned int * PU32;
  154.  
  155. typedef unsigned int U32;
  156.  
  157. /************************************************************************/
  158. /* 线性表数据结构,存储数组元素、数组大小、数组增长因子,下面简称为动态数组 */
  159. /************************************************************************/
  160. typedef struct _tag_ArrayList
  161. {
  162. PU32 container; // 存储数组元素的容器
  163. int length; // 数组长度
  164. int capacity; // 数组增长因子
  165. } TArrayList;
  166.  
  167. // 检查索引范围
  168. static TArrayList * checkRange(ArrayList *list, int pos);
  169.  
  170. ArrayList * ArrayList_CreateDefault()
  171. {
  172. return ArrayList_Create(DEFAULT_CAPACITY);
  173. }
  174.  
  175. ArrayList * ArrayList_Create(int capacity)
  176. {
  177. if (capacity < 0)
  178. {
  179. printf("fun ArrayList_Create error, Illegal capacity: %d", capacity);
  180. return NULL;
  181. }
  182.  
  183. TArrayList *list = (TArrayList *)malloc(sizeof(TArrayList));
  184. if (list == NULL)
  185. {
  186. printf("out of memory, create ArrayList fail!");
  187. return NULL;
  188. }
  189.  
  190. list->capacity = capacity;
  191. list->length = 0;
  192. list->container = (PU32)malloc(sizeof(PU32)* DEFAULT_CAPACITY);
  193. if (list->container == NULL)
  194. {
  195. printf("out of memory, create Array container fail!");
  196. return NULL;
  197. }
  198. memset(list->container, 0, sizeof(PU32)* DEFAULT_CAPACITY);
  199.  
  200. return list;
  201. }
  202.  
  203. int ArrayList_AddItem(ArrayList *list, ArrayItem *item, int pos)
  204. {
  205. int ret = 0;
  206. TArrayList *arrayList = NULL;
  207. if (list == NULL || item == NULL)
  208. {
  209. ret = -1;
  210. printf("fun ArrayList_AddItem error:%d of the list is NULL or item is NULL\n", ret);
  211. return ret;
  212. }
  213.  
  214. arrayList = (TArrayList *)list;
  215.  
  216. // 容错处理,如果当前存储了3个元素,要在第5个位置插入一个元素,将插入位置改成第4个位置
  217. // |0|1|2|3| | |
  218. if (pos > arrayList->length)
  219. {
  220. pos = arrayList->length;
  221. }
  222.  
  223. // 扩容
  224. PU32 newContainer = NULL;
  225. if (arrayList->length > 0 && arrayList->length % arrayList->capacity == 0)
  226. {
  227. newContainer = (PU32)malloc(sizeof(PU32)* (arrayList->length + arrayList->capacity));
  228. if (newContainer == NULL)
  229. {
  230. ret = -2;
  231. printf("out of memory, add item fail!");
  232. return ret;
  233. }
  234. memset(newContainer, 0, arrayList->length * sizeof(PU32));
  235.  
  236. // 将旧的数据拷贝到新的容器中
  237. memcpy(newContainer, arrayList->container, arrayList->length * sizeof(PU32));
  238.  
  239. // 释放旧容器的内存
  240. free(arrayList->container);
  241. // 指向新容器的地址
  242. arrayList->container = newContainer;
  243. }
  244.  
  245. // 将元素插入到数组pos位置
  246. for (int i = arrayList->length; i > pos; i--)
  247. {
  248. arrayList->container[i] = arrayList->container[i - 1];
  249. }
  250.  
  251. arrayList->container[pos] = (U32)item;
  252. arrayList->length++; // 长度+1
  253.  
  254. return ret;
  255. }
  256.  
  257. int ArrayList_AddItemBack(ArrayList *list, ArrayItem *item)
  258. {
  259. int ret = 0;
  260. TArrayList *arrayList = NULL;
  261. if (list == NULL || item == NULL)
  262. {
  263. ret = -1;
  264. printf("fun ArrayList_AddItemBack error:%d, of the list or item is NULL.\n");
  265. return ret;
  266. }
  267. arrayList = (TArrayList *)list;
  268.  
  269. return ArrayList_AddItem(list, item, arrayList->length);
  270. }
  271.  
  272. ArrayItem * ArrayList_RemoveItem(ArrayList *list, int pos)
  273. {
  274. TArrayList *arrayList = NULL;
  275. ArrayItem *arrayItem = NULL;
  276. arrayList = checkRange(list, pos);
  277. if (arrayList == NULL)
  278. {
  279. return NULL;
  280. }
  281.  
  282. // 缓存删除的元素
  283. arrayItem = (ArrayItem *)arrayList->container[pos];
  284.  
  285. // 从数组中移徐指定索引的元素
  286. for (int i = pos; i < arrayList->length; i++)
  287. {
  288. arrayList->container[i] = arrayList->container[i + 1];
  289. }
  290. arrayList->length--; // 长度减1
  291.  
  292. return arrayItem; // 返回被删除的元素
  293. }
  294.  
  295. int ArrayList_Clear(ArrayList *list)
  296. {
  297. int ret = 0;
  298. TArrayList *arrayList = NULL;
  299. if (list == NULL)
  300. {
  301. ret = -1;
  302. printf("fun ArrayList_Clear error:%d, of the list is NULL.\n");
  303. return ret;
  304. }
  305.  
  306. arrayList = (TArrayList *)list;
  307. while (arrayList->length)
  308. {
  309. arrayList->container[--arrayList->length] = 0;
  310. }
  311. return 0;
  312. }
  313.  
  314. int ArrayList_SetItem(ArrayList *list, ArrayItem *item, int pos)
  315. {
  316. int ret = 0;
  317. TArrayList *arrayList = NULL;
  318. arrayList = checkRange(list, pos);
  319. if (arrayList == NULL || item == NULL)
  320. {
  321. ret = -1;
  322. printf("fun ArrayList_SetItem error:%d, of the list or item is NULL.\n",ret);
  323. return ret;
  324. }
  325. arrayList->container[pos] = (U32)item;
  326. return 0;
  327. }
  328.  
  329. ArrayItem * ArrayList_GetItem(ArrayList *list, int pos)
  330. {
  331. TArrayList *arrayList = NULL;
  332. arrayList = checkRange(list, pos);
  333. if (arrayList == NULL)
  334. {
  335. return NULL;
  336. }
  337.  
  338. return (ArrayItem *)arrayList->container[pos];
  339. }
  340.  
  341. void ArrayList_For_Each(ArrayList *list, _Each_Function _fun_callback)
  342. {
  343. TArrayList *arrayList = NULL;
  344. if (list == NULL || _fun_callback == NULL)
  345. {
  346. printf("fun ArrayList_For_Each error, list or _fun_callback is NULL.\n");
  347. return;
  348. }
  349. arrayList = (TArrayList *)list;
  350. for (int i = 0; i < arrayList->length; i++)
  351. {
  352. _fun_callback((ArrayItem *)arrayList->container[i], i);
  353. }
  354. };
  355.  
  356. void ArrayList_For_Each_Range(ArrayList *list, int begin, int end,
  357. _Each_Function _fun_callback)
  358. {
  359. TArrayList *arrayList = NULL;
  360. if (list == NULL || _fun_callback == NULL)
  361. {
  362. printf("fun ArrayList_For_Each_Range error, list or _fun_callback is NULL.\n");
  363. return;
  364. }
  365.  
  366. arrayList = (TArrayList *)list;
  367.  
  368. if ((begin < 0 || begin > end) ||
  369. (end >= arrayList->length || end < begin))
  370. {
  371. printf("fun ArrayList_For_Each_Range error, pos out range:%d-%d", begin, end);
  372. return;
  373. }
  374.  
  375. for (int i = begin; i < end; i++)
  376. {
  377. _fun_callback((ArrayItem *)arrayList->container[i], i);
  378. }
  379. };
  380.  
  381. int ArrayList_GetLength(ArrayList * list)
  382. {
  383. TArrayList *arrayList = NULL;
  384. if (list == NULL)
  385. {
  386. printf("fun ArrayList_GetLength error, list is NULL.\n");
  387. return -1;
  388. }
  389.  
  390. arrayList = (TArrayList *)list;
  391.  
  392. return arrayList->length;
  393. }
  394.  
  395. int ArrayList_GetCapacity(ArrayList * list)
  396. {
  397. TArrayList *arrayList = NULL;
  398. if (list == NULL)
  399. {
  400. printf("fun ArrayList_GetCapacity error, list is NULL.\n");
  401. return -1;
  402. }
  403.  
  404. arrayList = (TArrayList *)list;
  405.  
  406. return arrayList->capacity;
  407. }
  408.  
  409. int ArrayList_Destory(ArrayList **list)
  410. {
  411. int ret = 0;
  412. if (list == NULL)
  413. {
  414. ret = -1;
  415. printf("fun ArrayList_Destory error:%d from (list == NULL)\n", ret);
  416. return ret;
  417. }
  418.  
  419. TArrayList *arrayList = (TArrayList *)*list;
  420.  
  421. if (arrayList->container != NULL)
  422. {
  423. free(arrayList->container);
  424. arrayList->container = NULL;
  425. }
  426.  
  427. free(arrayList);
  428. *list = NULL;
  429.  
  430. return ret;
  431. }
  432.  
  433. static TArrayList * checkRange(ArrayList *list, int pos)
  434. {
  435. TArrayList *arrayList = NULL;
  436. if (list == NULL)
  437. {
  438. printf("fun checkRange error, of the list is NULL.\n");
  439. return NULL;
  440. }
  441.  
  442. arrayList = (TArrayList *)list;
  443. if (pos < 0 || pos >= arrayList->length)
  444. {
  445. printf("fun checkRange error, of the pos:%d out range.\n", pos);
  446. return NULL;
  447. }
  448.  
  449. return arrayList;
  450. }
  451.  
  452. //-------------------------测试用例--------------------------------//
  453. #include <stdio.h>
  454. #include <stdlib.h>
  455. #include "ArrayList.h"
  456.  
  457. typedef struct Person {
  458. int age;
  459. char name[20];
  460. }Person;
  461.  
  462. void EachArrayCallback(ArrayItem *item, int pos)
  463. {
  464. Person *p = (Person *)item;
  465. printf("%d-->age=%d\n",pos,p->age);
  466. }
  467.  
  468. void main1()
  469. {
  470. int ret = -65535;
  471. ArrayList *list = NULL;
  472. list = ArrayList_Create(2);
  473.  
  474. Person p1, p2, p3, p4, p5,p6,p7,p8;
  475. p1.age = 10;
  476. p2.age = 20;
  477. p3.age = 30;
  478. p4.age = 40;
  479. p5.age = 50;
  480. p6.age = 60;
  481. p7.age = 70;
  482. p8.age = 80;
  483.  
  484. //ArrayList_AddItem(list, 100, 7);
  485. ArrayList_AddItem(list, &p1, 0);
  486. ArrayList_AddItem(list, &p2, 0);
  487. ArrayList_AddItem(list, &p3, 0);
  488. ArrayList_AddItem(list, &p4, 0);
  489. ArrayList_AddItem(list, &p5, 0);
  490. ArrayList_AddItem(list, &p6, 0);
  491. ArrayList_AddItemBack(list, &p7);
  492. ArrayList_AddItemBack(list, &p8);
  493.  
  494. printf("数组长度:%d\n", ArrayList_GetLength(list));
  495.  
  496. ArrayItem *item = ArrayList_RemoveItem(list, 2);
  497. printf("删除索引2位置的元素:%d\n",((Person *)item)->age);
  498.  
  499. for (int i = 0; i < ArrayList_GetLength(list); i++)
  500. {
  501. Person *p = (Person *)ArrayList_GetItem(list, i);
  502. printf("age=%d\n", p->age);
  503. }
  504.  
  505. Person pp = {100,"zhangsan"};
  506. ArrayList_SetItem(list, &pp, 1); // 修改索引位置为1的元素
  507. Person *p = (Person *)ArrayList_GetItem(list,1);
  508. if (p != NULL)
  509. {
  510. printf("age=%d\n",p->age);
  511. }
  512.  
  513. printf("\n---------------------foreach回调函数遍历数组------------------\n");
  514. ArrayList_For_Each(list, EachArrayCallback);
  515.  
  516. // 遍历指定范围内的元素
  517. printf("\n---------------foreach遍历指定范围内的数组元素------------------\n");
  518. ArrayList_For_Each_Range(list, 2, 5, EachArrayCallback);
  519.  
  520. // 清徐数组所有元素
  521. ret = ArrayList_Clear(list);
  522.  
  523. printf("arraylist length: %d\n", ArrayList_GetLength(list));
  524.  
  525. ret = ArrayList_Destory(&list);
  526.  
  527. system("pause");
  528. }
  529.  
  530. void fun(ArrayItem *item, int pos)
  531. {
  532. printf("%d--%d\n",pos,(unsigned int)item);
  533. }
  534.  
  535. void main()
  536. {
  537. ArrayList *list = NULL;
  538. list = ArrayList_Create(5);
  539.  
  540. ArrayList_AddItem(list, (ArrayItem *)10, 0);
  541. ArrayList_AddItem(list, (ArrayItem *)20, 0);
  542. ArrayList_AddItem(list, (ArrayItem *)30, 0);
  543. ArrayList_AddItem(list, (ArrayItem *)40, 0);
  544. ArrayList_AddItem(list, (ArrayItem *)50, 0);
  545. ArrayList_AddItemBack(list, (ArrayItem *)60);
  546.  
  547. printf("delete-->%d\n",(unsigned int)ArrayList_RemoveItem(list, 0));
  548.  
  549. ArrayList_For_Each(list, fun);
  550.  
  551. ArrayList_Destory(&list);
  552.  
  553. system("pause");
  554. }

线性表之顺序存储结构(C语言动态数组实现)的更多相关文章

  1. 线性表的顺序存储结构C语言版

    #include <stdio.h> #define MAXSIZE 101 #define N 10 typedef struct SeqList { int data[MAXSIZE] ...

  2. 数据结构4:顺序表(线性表的顺序存储结构)及C语言实现

    逻辑结构上呈线性分布的数据元素在实际的物理存储结构中也同样相互之间紧挨着,这种存储结构称为线性表的顺序存储结构. 也就是说,逻辑上具有线性关系的数据按照前后的次序全部存储在一整块连续的内存空间中,之间 ...

  3. 2.2_线性表的顺序存储结构_参考集合ArrayList

    [线性表的顺序存储从结构] 指的是用一段连续的存储单元一次储存线性表的数据元素. [线性表的顺序存储的结构代码 C语言版] #define MAXSIZE 20 /*存储空间初始分配量*/ typed ...

  4. 线性表的顺序存储结构——java

    线性表的顺序存储结构:是指用一组地址连续的存储单元一次存放线性表的元素.为了使用顺序结构实现线性表,程序通常会采用数组来保存线性中的元素,是一种随机存储的数据结构,适合随机访问.java中ArrayL ...

  5. C++编程练习(1)----“实现简单的线性表的顺序存储结构“

    线性表的顺序存储结构,指的是用一段地址连续的存储单元依次存储线性表的数据元素. 故可以用数组来实现顺序存储结构. 用C++编写的利用数组实现简单的读取.插入和删除功能的线性表. #include< ...

  6. 线性表的顺序存储结构之顺序表类的实现_Java

    在上一篇博文——线性表接口的实现_Java中,我们实现了线性表的接口,今天让我们来实现线性表的顺序存储结构——顺序表类. 首先让我们来看下顺序表的定义: 线性表的顺序存储是用一组连续的内存单元依次存放 ...

  7. c数据结构 -- 线性表之 顺序存储结构 于 链式存储结构 (单链表)

    线性表 定义:线性表是具有相同特性的数据元素的一个有限序列 类型: 1:顺序存储结构 定义:把逻辑上相邻的数据元素存储在物理上相邻的存储单元中的存储结构 算法: #include <stdio. ...

  8. c语言数据结构之线性表的顺序存储结构

    线性表,即线性存储结构,将具有“一对一”关系的数据“线性”地存储到物理空间中,这种存储结构就称为线性存储结构,简称线性表. 注意:使用线性表存储的数据,要求数据类型必须一致,线性表存储的数据,要么全不 ...

  9. 线性表 linear_list 顺序存储结构

    可以把线性表看作一串珠子 序列:指其中的元素是有序的 注意last和length变量的内在关系 注意:将元素所占的空间和表长合并为C语言的一个结构类型 静态分配的方式,分配给一个固定大小的存储空间之后 ...

随机推荐

  1. poj 1226

    跟3294比较类似,但是不需要输出具体的串,比较简单,只要把串反转连接上去解法就一样了. #include <iostream> #include <cstdio> #incl ...

  2. PowerMock注解PowerMockIgnore的使用方法

    故事要从一个异常开始,某天我在开发一个加密.解密特性,算法使用的是3DES,样例代码如下. package org.jackie.study.powermock; import java.io.Uns ...

  3. [D3] 3. Scaling Basics

    d3.scale.linear() <!DOCTYPE html> <html> <head lang="en"> <meta chars ...

  4. Android仿微信UI布局视图(圆角布局的实现)

    圆角button.或布局能够在xml文件里实现,但也能够使用图片直接达到所需的效果,曾经版本号的微信就使用了这样的方法. 实现效果图:    watermark/2/text/aHR0cDovL2Js ...

  5. myeclipse设置技巧

    如何设置jsp的默认打开为MyEclipse JSP Editor? windows -> General -> Editor - > File Associations 选择 *. ...

  6. CSS hack常用方案(摘选)

    邮箱因为默认了line-height?:170%,导致采用table元素时继承问题,可以采用line-height:50% 很好解决. 常 在使用float时,后面的显示不正常,因为继承了float了 ...

  7. IHttpModule实现URL重写

    1.用自定义IHttpModule实现URL重写 一般来说,要显示一些动态数据总是采用带参数的方式,比如制作一个UserInfo.aspx的动态页面用于显示系统的UserInfo这个用户信息表的数据, ...

  8. 文件权限和目录权限详解(rwx)

    [文件] r:可读,可以使用cat命令查看文件内容: w:可写,可以编辑或删除文件: x:可执行,可以当作命令提交给内核 [目录] r:可以对此目录执行ls,列出内部所有文件 w:可以在此目录创建文件 ...

  9. maven 创建web项目

    1,新建一个web项目 2,构建基础目录 web.xml <!DOCTYPE web-app PUBLIC  "-//Sun Microsystems, Inc.//DTD Web A ...

  10. C# 引用SHDocVw 实现模拟网页操作

    因为最近项目需要,所以接触到了网页爬取. 1. HttpWebRequest 初期接触的都是一些比较简单的网页,通过Fiddler抓包分析后,就能模拟进行http请求,进行想要的操作. 2. WebB ...