Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. /*
  2. * singly_linked_list.c
  3. *
  4. * Created on: Oct 16, 2019
  5. * Author: Bojan Rikic RA134/2016
  6. */
  7.  
  8. #include "singly_linked_list.h"
  9.  
  10. Node* create_node(int_least16_t new_value)
  11. {
  12. Node* current_node = malloc(sizeof(Node));
  13. if (!current_node)
  14. {
  15. printf("No memory !\n");
  16. return (Node*) NULL;
  17. }
  18. current_node->value = new_value;
  19. current_node->next = (Node*) NULL;
  20. return current_node;
  21. }
  22.  
  23. List* make_list(void)
  24. {
  25. List* initalize_singly_linked_list = malloc(sizeof(List));
  26. if (!initalize_singly_linked_list)
  27. {
  28. printf("No memory !\n");
  29. return (List*) NULL;
  30. }
  31. initalize_singly_linked_list->head = (Node*) NULL;
  32. return initalize_singly_linked_list;
  33. }
  34.  
  35.  
  36. void push_begin(List* singly_linked_list, int_least16_t n)
  37. {
  38. assert(n < CHAR_MAX);
  39. assert(n > CHAR_MIN);
  40. assert(n < SHRT_MAX);
  41. Node* new_node_for_push_to_begin = (Node*) malloc(sizeof(Node));
  42. new_node_for_push_to_begin->value = n;
  43. new_node_for_push_to_begin->next = singly_linked_list->head;
  44. singly_linked_list->head = new_node_for_push_to_begin;
  45. }
  46. void insert_in_list(const List* singly_linked_list, const Node *position,int_least16_t n)
  47. {
  48. assert(n < CHAR_MAX);
  49. assert(n > CHAR_MIN);
  50. assert(n < SHRT_MAX);
  51. Node* new_node = (Node*) NULL;
  52. Node* tmp_node = (Node*) NULL;
  53.  
  54. if (position == (Node*) NULL)
  55. {
  56. printf("No memory !\n");
  57. return;
  58. }
  59.  
  60. new_node = create_node(n);
  61.  
  62. tmp_node = singly_linked_list->head;
  63.  
  64. while(tmp_node->next != position->next)
  65. {
  66. tmp_node = tmp_node->next;
  67. }
  68.  
  69. new_node->next = tmp_node->next;
  70. tmp_node->next = new_node;
  71. }
  72.  
  73. void push_end(List* singly_linked_list, int_least16_t n)
  74. {
  75. assert(n < CHAR_MAX);
  76. assert(n > CHAR_MIN);
  77. assert(n < SHRT_MAX);
  78. Node* new_node_for_push_to_end = (Node*) NULL;
  79. if(singly_linked_list->head == (Node*) NULL)
  80. {
  81. singly_linked_list->head = create_node(n);
  82. }
  83. else
  84. {
  85. new_node_for_push_to_end = singly_linked_list->head;
  86. while (new_node_for_push_to_end->next != (Node*) NULL)
  87. {
  88. new_node_for_push_to_end = new_node_for_push_to_end->next;
  89. }
  90. new_node_for_push_to_end->next = create_node(n);
  91. }
  92. }
  93.  
  94. void print_list(const List* singly_linked_list)
  95. {
  96. Node* current_node_for_print = singly_linked_list->head;
  97. if(singly_linked_list->head == (Node*) NULL)
  98. {
  99. printf("List is empty!\n");
  100. return;
  101. }
  102. printf("List --> ");
  103. while(current_node_for_print != NULL)
  104. {
  105. printf("%"PRIdLEAST16",", current_node_for_print->value);
  106. current_node_for_print = current_node_for_print->next;
  107. }
  108. printf("\n");
  109. }
  110.  
  111. int_least8_t pop_last(List* singly_linked_list){
  112. int_least8_t last_element;
  113. Node* toDelete;
  114. Node* secondLastNode;
  115.  
  116. if(singly_linked_list->head == (Node*) NULL)
  117. {
  118. return (int_least8_t) 0;
  119. }
  120. else
  121. {
  122. toDelete = singly_linked_list->head;
  123. secondLastNode = singly_linked_list->head;
  124.  
  125. while(toDelete->next != (Node*) NULL)
  126. {
  127. secondLastNode = toDelete;
  128. toDelete = toDelete->next;
  129. }
  130.  
  131. if(toDelete == singly_linked_list->head)
  132. {
  133. singly_linked_list->head = (Node*) NULL;
  134. }
  135. else
  136. {
  137. secondLastNode->next = (Node*) NULL;
  138. }
  139. last_element = toDelete->value;
  140. free(toDelete);
  141. }
  142. return last_element;
  143. }
  144.  
  145.  
  146. void clear_list(List* singly_linked_list)
  147. {
  148. Node* current_node_for_clear_list;
  149.  
  150. while(singly_linked_list->head != (Node*) NULL)
  151. {
  152. current_node_for_clear_list = singly_linked_list->head;
  153. singly_linked_list->head = current_node_for_clear_list->next;
  154. free(current_node_for_clear_list);
  155. }
  156. }
  157.  
  158. void split_list(List* singly_linked_list, List* list_with_even_index, List* list_with_odd_index)
  159. {
  160.  
  161. uint_least8_t i = (uint_least8_t) 0U;
  162. Node* node_odd = singly_linked_list->head;
  163. Node* temp;
  164.  
  165. Node* node_odd_temp = list_with_odd_index->head;
  166. Node* node_even_temp = list_with_even_index->head;
  167.  
  168. while(node_odd != (Node*) NULL)
  169. {
  170. temp = create_node(node_odd->value);
  171.  
  172. i++;
  173. if((i % (uint_least8_t) 2U) == (uint_least8_t) 1U)
  174. {
  175. node_odd_temp = temp;
  176. push_end(list_with_odd_index, node_odd_temp->value);
  177. }
  178. else
  179. {
  180. node_even_temp = temp;
  181. push_end(list_with_even_index, node_even_temp->value);
  182. }
  183. node_odd = node_odd->next;
  184. }
  185. clear_list(singly_linked_list);
  186. node_odd_temp->next = (Node*) NULL;
  187. node_even_temp->next = (Node*) NULL;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement