Advertisement
Guest User

Untitled

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