Advertisement
Guest User

podles

a guest
Nov 22nd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. struct List * list_create (void) {
  2. struct List* temp;
  3. temp = (struct List*) calloc(1, sizeof(struct List));
  4.  
  5. temp->head = NULL;
  6.  
  7. return temp;
  8. }
  9.  
  10. void list_add_first (struct List * list, Data x) {
  11. struct Node* temp;
  12.  
  13. temp = (struct Node*) calloc(1, sizeof(struct Node));
  14. temp->next = list->head;
  15. list->head = temp;
  16. list->head->val = x;
  17. }
  18. void list_add_last (struct List * list, Data x) {
  19. struct Node* temp;
  20. struct Node* last = list->head;
  21.  
  22. temp = (struct Node*) calloc(1, sizeof(struct Node));
  23. temp->val = x;
  24. temp->next = NULL;
  25.  
  26. while(last->next != NULL)
  27. last = last->next;
  28.  
  29. last->next = temp;
  30. }
  31.  
  32. Data list_remove_first (struct List * list) {
  33. Data val;
  34. struct Node* temp;
  35.  
  36. val = list->head->val;
  37.  
  38. temp = list->head;
  39. list->head = list->head->next;
  40. free(temp);
  41.  
  42. return val;
  43. }
  44. Data list_remove_last (struct List * list) {
  45. Data val;
  46. struct Node* temp = list->head;
  47.  
  48. while(temp->next != NULL) {
  49. temp = temp->next;
  50. }
  51.  
  52. val = temp->val;
  53. free(temp);
  54.  
  55. return val;
  56. }
  57.  
  58. Data list_get_first (struct List * list) {
  59. return list->head->val;
  60. }
  61.  
  62. Data list_get_last (struct List * list) {
  63. struct Node* temp = list->head;
  64.  
  65. while (temp->next != NULL) {
  66. temp = temp->next;
  67. }
  68.  
  69. return temp->val;
  70. }
  71.  
  72. void list_print (struct List * list) {
  73. struct Node* tree = list->head;
  74.  
  75. if (list->head == NULL) {
  76. printf("Empty list\n");
  77. return ;
  78. }
  79.  
  80. while (tree->next != NULL && tree != NULL) {
  81. printf("%d ", tree->val);
  82. tree = tree->next;
  83. }
  84. printf("%d \n", tree->val);
  85. }
  86.  
  87. int list_size(struct List * list) {
  88. struct Node* tree = list->head;
  89. unsigned int size = 1;
  90.  
  91. if (tree == NULL) { ///empty list
  92. return 0;
  93. }
  94.  
  95. while (tree->next != NULL) {
  96. size++;
  97. tree = tree->next;
  98. }
  99.  
  100. return size;
  101. }
  102.  
  103. void list_clear(struct List * list) {
  104. struct Node* base = list->head;
  105. struct Node* step = NULL;
  106.  
  107. if (base == NULL) /// empty already
  108. return ;
  109.  
  110. while (base->next != NULL) {
  111. step = base->next;
  112. free(base);
  113. base = step;
  114. step = base->next;
  115. }
  116.  
  117. free(base);
  118.  
  119. }
  120.  
  121. void list_destroy (struct List * list) {
  122. list_clear(list);
  123. free(list);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement