文档章节

链表

 菜鸟多指教
发布于 2016/10/20 22:04
字数 421
阅读 1
收藏 0

    list.h 

  1. #ifndef _LIST_H_
  2. #define _LIST_H_
  3.  
  4. #define LIST_SIZE 10
  5.  
  6. enum LIST_TYPE
  7. {
  8. LIST_ERROR = -1,
  9. LIST_OK
  10. };
  11.  
  12. typedef int data_t;
  13. typedef struct List List;
  14.  
  15. List *CreateList();
  16. void DestoryList(List *pList);
  17. int InsertItemList(List *pList, data_t tData, int iOffset);
  18. int DelItem(List *pList, data_t *pData, int iOffset);
  19. int UpdateItem(List *pList, data_t tNewData, data_t tOldData);
  20. int SearchItem(List *pList, data_t tData);
  21. void ShowList(List *pList);
  22. #endif //_LIST_H_

list.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "list.h"
  5.  
  6. struct List
  7. {
  8. data_t data;
  9. struct List *pNext;
  10. };
  11.  
  12. List *CreateList()
  13. {
  14. List *pList = NULL;
  15. pList = (List *) malloc (sizeof(List));
  16. if (NULL == pList)
  17. {
  18. return pList;
  19. }
  20. memset(pList, 0, sizeof(List));
  21. pList->pNext = NULL;
  22. return pList;
  23. }
  24.  
  25. void DestoryList(List *pList)
  26. {
  27. if (NULL == pList)
  28. {
  29. return;
  30. }
  31. //free data node
  32. List *pNode = pList->pNext;
  33. List *pTmp = NULL;
  34. while(NULL != pNode)
  35. {
  36. pTmp = pNode->pNext;
  37. free(pNode);
  38. pNode = pTmp;
  39. }
  40. //free Header
  41. free(pList);
  42. pList = NULL;
  43. }
  44.  
  45. int InsertItemList(List *pList, data_t tData, int iOffset)
  46. {
  47. if (NULL == pList)
  48. {
  49. return LIST_ERROR;
  50. }
  51. //create link node
  52. List *pNode = (List *) malloc (sizeof(List));
  53. if (NULL == pNode)
  54. {
  55. return LIST_ERROR;
  56. }
  57. memset(pNode, 0, sizeof(List));
  58. pNode->data = tData;
  59. pNode->pNext = NULL;
  60. //insert node
  61. pNode->pNext = pList->pNext;
  62. pList->pNext = pNode;
  63. return LIST_OK;
  64. }
  65.  
  66. int DelItem(List *pList, data_t *pData, int iOffset)
  67. {
  68. if (NULL == pList || NULL == pData)
  69. {
  70. return LIST_ERROR;
  71. }
  72. //free first data node
  73. List *pDel = pList->pNext;
  74. if (NULL != pDel)
  75. {
  76. *pData = pDel->data;
  77. pList->pNext = pDel->pNext;
  78. free(pDel);
  79. pDel = NULL;
  80. }
  81. return LIST_OK;
  82. }
  83.  
  84. int UpdateItem(List *pList, data_t tNewData, data_t tOldData)
  85. {
  86. if (NULL == pList)
  87. {
  88. return LIST_ERROR;
  89. }
  90. List *pNode = pList->pNext;
  91. while(NULL != pNode)
  92. {
  93. if (pNode->data == tOldData)
  94. {
  95. pNode->data = tNewData;
  96. return LIST_OK;
  97. }
  98. pNode = pNode->pNext;
  99. }
  100. return LIST_ERROR;
  101. }
  102.  
  103. int SearchItem(List *pList, data_t tData)
  104. {
  105. if (NULL == pList)
  106. {
  107. return LIST_ERROR;
  108. }
  109. List *pNode = pList->pNext;
  110. while(NULL != pNode)
  111. {
  112. if (pNode->data == tData)
  113. {
  114. return LIST_OK;
  115. }
  116. pNode = pNode->pNext;
  117. }
  118. return LIST_ERROR;
  119. }
  120.  
  121. void ShowList(List *pList)
  122. {
  123. if (NULL == pList)
  124. {
  125. return;
  126. }
  127. List *pTmp = pList->pNext;
  128. while(NULL != pTmp)
  129. {
  130. printf("%d ", pTmp->data);
  131. pTmp = pTmp->pNext;
  132. }
  133. printf("\r\n");
  134. }

test.c

  1. #include <stdio.h>
  2. #include "list.h"
  3.  
  4. int main()
  5. {
  6. //create
  7. List *pList = CreateList();
  8. if (NULL == pList)
  9. {
  10. return -1;
  11. }
  12. printf("create ok\r\n");
  13. //insert
  14. int i = 5;
  15. int iRet = -1;
  16. while(i--)
  17. {
  18. iRet = InsertItemList(pList, i, 0);
  19. if (LIST_ERROR == iRet)
  20. {
  21. printf("insert error\r\n");
  22. break;
  23. }
  24. }
  25. //show list
  26. ShowList(pList);
  27. //delete item
  28. data_t tData = -1;
  29. iRet = DelItem(pList, &tData, 2);
  30. printf("iret %d tdata %d\r\n", iRet, tData);
  31. ShowList(pList);
  32. //search
  33. iRet = SearchItem(pList, 3);
  34. printf("index %d\r\n", iRet);
  35. //update
  36. iRet = UpdateItem(pList, 8, 1);
  37. printf("update iret %d\r\n", iRet);
  38. //pList->iCount = 100;
  39. ShowList(pList);
  40. //destory list
  41. DestoryList(pList);
  42. pList = NULL;
  43. return 0;
  44. }

 

  1.  

© 著作权归作者所有

粉丝 0
博文 1
码字总数 421
作品 0
程序员
私信 提问

暂无文章

typescript 接口 函数类型 可索引类型

函数类型 可索引类型 数字索引签名 字符串索引签名 数字索引签名返回值 必须是 字符串索引签名返回值的子集 只读索引签名

lilugirl
今天
3
0
Oracle SQL语法实例合集

如需转载请注明出处https://my.oschina.net/feistel/blog/3052024 目的:迅速激活Oracle SQL 参考:《Oracle从入门到精通》 ------------------------------------------------------------......

LoSingSang
今天
2
0
增加 PostgreSQL 服务进程的最大打开文件数

https://serverfault.com/questions/628610/increasing-nproc-for-processes-launched-by-systemd-on-centos-7 要在systemd的配置里加才行...

helloclia
今天
2
0
组合模式在商品分类列表中的应用

在所有的树形结构中最适合的设计模式就是组合模式,我们看看常用商品分类中如何使用。 先定义一个树形结构的商品接口 public interface TreeProduct { List<TreeProduct> allProducts(...

算法之名
今天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部