Guest User

Untitled

a guest
Jul 22nd, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define TLIST_CONTENT int
  4. #define TRUE 1
  5. #define FALSE 0
  6. #define BOOLEAN short
  7.  
  8. struct slist {
  9. struct stlistitem *head, *tail;
  10. int elem_count;
  11. };
  12.  
  13. typedef struct slist tlist;
  14.  
  15. struct stlistitem {
  16. struct stlistitem *next, *previous;
  17. TLIST_CONTENT *content;
  18. };
  19.  
  20. typedef struct stlistitem tlistitem;
  21.  
  22. tlist *tlist_init();
  23. void tlist_add(tlist *list, TLIST_CONTENT *content);
  24. void tlist_free(tlist *list);
  25. tlistitem *_tlist_new_item();
  26.  
  27. int main(int argc, char *argv[]) {
  28. tlist *a;
  29. TLIST_CONTENT value1, value2;
  30. value1 = 99;
  31. value2 = 88;
  32. a = tlist_init();
  33. tlist_add(a,&value1);
  34. tlist_add(a,&value2);
  35. printf("%d\n", *(a->head->next->content));
  36. printf("%d\n", *(a->head->next->next->content));
  37. tlist_free(a);
  38. }
  39.  
  40. tlist *tlist_init() {
  41. tlist *l;
  42. l = (tlist *)malloc(sizeof(tlist));
  43. tlistitem *head;
  44. head = _tlist_new_item();
  45. l->head = head;
  46. l->tail = l->head;
  47. l->elem_count = 0;
  48. return l;
  49. }
  50.  
  51. void tlist_add(tlist *list, TLIST_CONTENT *content) {
  52. tlistitem *new_item, *temp;
  53. new_item = (tlistitem *)malloc(sizeof(tlistitem));
  54. list->tail->next = new_item;
  55. new_item->previous = list->tail;
  56. new_item->next = NULL;
  57. new_item->content = content;
  58. list->tail = new_item;
  59. list->elem_count++;
  60. }
  61.  
  62. tlistitem *_tlist_new_item() {
  63. tlistitem *item;
  64. item = (tlistitem *)malloc(sizeof(tlistitem));
  65. item->previous = NULL;
  66. item->next = NULL;
  67. item->content = NULL;
  68. return item;
  69. }
  70.  
  71. tlistitem *tlist_get_head(tlist *list) {
  72. return list->head->next;
  73. }
  74.  
  75. tlistitem *tlist_get_tail(tlist *list) {
  76. return list->tail;
  77. }
  78.  
  79. tlistitem *tlist_get_next(tlistitem *item) {
  80. return item->next;
  81. }
  82.  
  83. tlistitem *tlist_get_previous(tlistitem *item) {
  84. return item->previous;
  85. }
  86.  
  87. BOOLEAN tlist_is_tail(tlist *list, tlistitem *item) {
  88. return (item == (list->tail));
  89. }
  90.  
  91. BOOLEAN tlist_is_head(tlist *list, tlistitem *item) {
  92. return (item == (list->head));
  93. }
  94.  
  95.  
  96. void tlist_free_item(tlistitem *item) {
  97. free(item->next);
  98. item->next = NULL;
  99. free(item->previous);
  100. item->previous = NULL;
  101. free(item);
  102. item = NULL;
  103. }
  104.  
  105. void tlist_free(tlist *list) {
  106. tlistitem *item = tlist_get_tail(list);
  107. tlistitem *prev;
  108. if(item != NULL) {
  109. prev = tlist_get_previous(item);
  110. while(!tlist_is_head(list,item)) {
  111. tlist_free_item(item);
  112. item = prev;
  113. prev = tlist_get_previous(item);
  114. }
  115. }
  116. free(list);
  117. }
Add Comment
Please, Sign In to add comment