当前位置:   article > 正文

数据结构-3(链式存储顺序表)

数据结构-3(链式存储顺序表)

概念

通常用一个tmp指针来标记定位

头文件

  1. #ifndef _DOUBLE_LINK_LIST
  2. #define _DOUBLE_LINK_LIST
  3. typedef struct preson{
  4. char name[32];
  5. char sex;
  6. int age;
  7. int score;
  8. }DATATYPE;
  9. typedef enum{
  10. FORWARD,
  11. BACKWARD,
  12. }DIRECT;
  13. typedef struct dou_node{
  14. DATATYPE data;
  15. struct dou_node *next, *prev;
  16. }double_link_node;
  17. typedef struct list{
  18. double_link_node *head;
  19. int clen;
  20. }double_link_list;
  21. typedef int (*pfun)(DATATYPE *data, void *arg);
  22. double_link_list *create_link_list(void);
  23. int insert_head_link_list(double_link_list *list, DATATYPE *data);
  24. int show_link_list(double_link_list *list, DIRECT dire);
  25. double_link_node *find_link_list(double_link_list *list, pfun fun, void *arg);
  26. int delete_link_list(double_link_list *list, pfun fun, void *arg);
  27. int revise_link_list(double_link_list *list, char *name, DATATYPE data);
  28. int destroy_link_list(double_link_list *list);
  29. int insert_tail_link_list(double_link_list *list, DATATYPE *data);
  30. int modify_link_list(double_link_list *list, pfun, void *arg, DATATYPE *data);
  31. int getsize_double_link_list(double_link_list *list);
  32. int insert_pos_link_list(double_link_list *list, DATATYPE *data, int pos);
  33. int isempty_link_list(double_link_list *list);
  34. int clear_link_list(double_link_list *list);
  35. int inverse_link_list(double_link_list *list);
  36. #endif

.c文件

  1. #include <stdio.h>
  2. #include "double_link_list.h"
  3. #include <string.h>
  4. #include <stdlib.h>
  5. double_link_list *create_link_list(void)
  6. {
  7. double_link_list *link = (double_link_list *)malloc(sizeof(double_link_list));
  8. if(NULL == link)
  9. {
  10. perror("double_link_list fail");
  11. return NULL;
  12. }
  13. link->head = NULL;
  14. link->clen = 0;
  15. return link;
  16. }
  17. int insert_head_link_list(double_link_list *list, DATATYPE *data)
  18. {
  19. double_link_node *newnode = (double_link_node *)malloc(sizeof(double_link_node));
  20. if(NULL == newnode)
  21. {
  22. perror("create newnode fail");
  23. return -1;
  24. }
  25. memcpy(&newnode->data, data, sizeof(DATATYPE));
  26. newnode->next = NULL;
  27. newnode->prev = NULL;
  28. if(NULL == list->head)
  29. {
  30. list->head = newnode;
  31. }
  32. else
  33. {
  34. newnode->next = list->head;
  35. list->head->prev = newnode;
  36. list->head = newnode;
  37. }
  38. list->clen++;
  39. return 0;
  40. }
  41. int getsize_double_link_list(double_link_list *list)
  42. {
  43. return list->clen;
  44. }
  45. int show_link_list(double_link_list *list, DIRECT dire)
  46. {
  47. int len = getsize_double_link_list(list);
  48. double_link_node *tmp = list->head;
  49. int i = 0;
  50. if(FORWARD == dire)
  51. {
  52. for(i = 0; i < len; ++i)
  53. {
  54. printf("%s %c %d %d\n", tmp->data.name, tmp->data.sex, tmp->data.age, tmp->data.score);
  55. tmp = tmp->next;
  56. }
  57. }
  58. else
  59. {
  60. while(tmp->next)
  61. {
  62. tmp=tmp->next;
  63. }
  64. for(i = 0; i < len; ++i)
  65. {
  66. printf("%s %c %d %d\n", tmp->data.name, tmp->data.sex, tmp->data.age, tmp->data.score);
  67. tmp = tmp->prev;
  68. }
  69. }
  70. return 0;
  71. }
  72. double_link_node *find_link_list(double_link_list *list, pfun fun, void *arg)
  73. {
  74. int len = getsize_double_link_list(list);
  75. double_link_node *tmp = list->head;
  76. int i = 0;
  77. for(i = 0; i < len; ++i)
  78. {
  79. if(fun(&tmp->data,arg))
  80. {
  81. return tmp;
  82. }
  83. tmp = tmp->next;
  84. }
  85. return NULL;
  86. }
  87. int isempty_link_list(double_link_list *list)
  88. {
  89. return 0 == list->clen;
  90. }
  91. int insert_tail_link_list(double_link_list *list, DATATYPE *data)
  92. {
  93. double_link_node *tmp = list->head;
  94. if(isempty_link_list(list))
  95. {
  96. return insert_head_link_list(list,data);
  97. }
  98. else
  99. {
  100. double_link_node *newnode = (double_link_node *)malloc(sizeof(double_link_node));
  101. if(NULL == newnode)
  102. {
  103. perror("create newnode_2 fail");
  104. return -1;
  105. }
  106. memcpy(&newnode->data, data, sizeof(DATATYPE));
  107. newnode->next = NULL;
  108. newnode->prev = NULL;
  109. while(tmp->next)
  110. {
  111. tmp = tmp->next;
  112. }
  113. newnode->prev = tmp;
  114. tmp->next = newnode;
  115. }
  116. list->clen++;
  117. return 0;
  118. }
  119. int insert_pos_link_list(double_link_list *list, DATATYPE *data, int pos)
  120. {
  121. double_link_node *newnode = (double_link_node *)malloc(sizeof(double_link_node));
  122. if(NULL == newnode)
  123. {
  124. perror("creat newnode_3 fail");
  125. return -1;
  126. }
  127. memcpy(&newnode->data, data, sizeof(DATATYPE));
  128. newnode->next = NULL;
  129. newnode->prev = NULL;
  130. double_link_node *tmp = list->head;
  131. int i = 0;
  132. for(i = 0; i < pos; ++i)
  133. {
  134. tmp = tmp->next;
  135. }
  136. newnode->next = tmp;
  137. newnode->prev = tmp->prev;
  138. tmp->prev = newnode;
  139. if(newnode->prev)
  140. {
  141. newnode->prev->next = newnode;
  142. }
  143. else
  144. {
  145. list->head = newnode;
  146. }
  147. list->clen++;
  148. return 0;
  149. }
  150. int delete_link_list(double_link_list *list, pfun fun, void *arg)
  151. {
  152. double_link_node *tmp = find_link_list(list, fun, arg);
  153. if(NULL == tmp)
  154. {
  155. return -1;
  156. }
  157. if(tmp->next)
  158. {
  159. tmp->next->prev = tmp->prev;
  160. }
  161. if(tmp->prev)
  162. {
  163. tmp->prev->next = tmp->next;
  164. }
  165. else
  166. {
  167. list->head = tmp->next;
  168. }
  169. list->clen--;
  170. free(tmp);
  171. return 0;
  172. }
  173. int modify_link_list(double_link_list *list, pfun fun, void *arg, DATATYPE *data)
  174. {
  175. double_link_node *tmp = find_link_list(list, fun, arg);
  176. if(NULL == tmp)
  177. {
  178. return -1;
  179. }
  180. memcpy(&tmp->data, data, sizeof(DATATYPE));
  181. return 0;
  182. }
  183. int clear_link_list(double_link_list *list)
  184. {
  185. double_link_node *tmp = list->head;
  186. int len = getsize_double_link_list(list);
  187. int i = 0;
  188. for(i = 0; i < len-1; ++i)
  189. {
  190. tmp = tmp->next;
  191. free(tmp->prev);
  192. }
  193. free(tmp);
  194. }
  195. int inverse_link_list(double_link_list *list)
  196. {
  197. double_link_node *tmp = NULL;
  198. int len = getsize_double_link_list(list);
  199. int i = 0;
  200. if(len > 2)
  201. {
  202. double_link_node *q;
  203. for(i = 0; i < len; ++i)
  204. {
  205. tmp = list->head;
  206. list->head = list->head->next;
  207. q = tmp->prev;
  208. tmp->prev = tmp->next;
  209. tmp->next = q;
  210. }
  211. list->head = tmp;
  212. }
  213. else
  214. {
  215. printf("it's not need\n");
  216. }
  217. return 0;
  218. }

主函数

  1. #include <stdio.h>
  2. #include "double_link_list.h"
  3. #include <string.h>
  4. int find_name(DATATYPE *data, void *arg)
  5. {
  6. return 0 == strcmp(data->name, (char *)arg);
  7. }
  8. int main()
  9. {
  10. double_link_list* link = create_link_list();
  11. DATATYPE data[]={
  12. {"zhansan",'m',20,90},
  13. {"lisi",'f',22,87},
  14. {"wangmazi",'m',21,93},
  15. {"guanerge",'m',40,60},
  16. {"liuei",'m',42,83},
  17. };
  18. insert_head_link_list(link, &data[0]);
  19. insert_head_link_list(link, &data[1]);
  20. insert_head_link_list(link, &data[2]);
  21. show_link_list(link, FORWARD);
  22. printf("----------------------------\n");
  23. show_link_list(link, BACKWARD);
  24. double_link_node *ret = find_link_list(link, find_name, "lisi");
  25. if(NULL == ret)
  26. {
  27. printf("can't find\n");
  28. }
  29. else
  30. {
  31. printf("find it, %s %d\n", ret->data.name, ret->data.score);
  32. }
  33. printf("----------------------------\n");
  34. insert_pos_link_list(link, &data[3], 2);
  35. show_link_list(link, BACKWARD);
  36. printf("----------------------------\n");
  37. delete_link_list(link, find_name, "lisi");
  38. show_link_list(link, BACKWARD);
  39. printf("------------modify----------------\n");
  40. modify_link_list(link, find_name, "zhansan", &data[4]);
  41. show_link_list(link, BACKWARD);
  42. printf("------------inverse----------------\n");
  43. inverse_link_list(link);
  44. show_link_list(link, BACKWARD);
  45. printf("------------ii----------------\n");
  46. clear_link_list(link);
  47. return 0;
  48. }

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

闽ICP备14008679号