You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
1.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "list.h"
  4. namespace DN{
  5. list *make_list()
  6. {
  7. list *l = (list*)malloc(sizeof(list));
  8. l->size = 0;
  9. l->front = 0;
  10. l->back = 0;
  11. return l;
  12. }
  13. /*
  14. void transfer_node(list *s, list *d, node *n)
  15. {
  16. node *prev, *next;
  17. prev = n->prev;
  18. next = n->next;
  19. if(prev) prev->next = next;
  20. if(next) next->prev = prev;
  21. --s->size;
  22. if(s->front == n) s->front = next;
  23. if(s->back == n) s->back = prev;
  24. }
  25. */
  26. void *list_pop(list *l){
  27. if (!l->back) return 0;
  28. node *b = l->back;
  29. void *val = b->val;
  30. l->back = b->prev;
  31. if (l->back) l->back->next = 0;
  32. free(b);
  33. --l->size;
  34. return val;
  35. }
  36. void list_insert(list *l, void *val)
  37. {
  38. node *nw = (node*)malloc(sizeof(node));
  39. nw->val = val;
  40. nw->next = 0;
  41. if (!l->back){
  42. l->front = nw;
  43. nw->prev = 0;
  44. }
  45. else{
  46. l->back->next = nw;
  47. nw->prev = l->back;
  48. }
  49. l->back = nw;
  50. ++l->size;
  51. }
  52. void free_node(node *n)
  53. {
  54. node *next;
  55. while (n) {
  56. next = n->next;
  57. free(n);
  58. n = next;
  59. }
  60. }
  61. void free_list(list *l)
  62. {
  63. free_node(l->front);
  64. free(l);
  65. }
  66. void free_list_contents(list *l)
  67. {
  68. node *n = l->front;
  69. while (n){
  70. free(n->val);
  71. n = n->next;
  72. }
  73. }
  74. void **list_to_array(list *l)
  75. {
  76. void **a = (void**)calloc(l->size, sizeof(void*));
  77. int count = 0;
  78. node *n = l->front;
  79. while (n){
  80. a[count++] = n->val;
  81. n = n->next;
  82. }
  83. return a;
  84. }
  85. };