Advertisement
informaticage

Linked lists - Insert at

Aug 19th, 2021
1,539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. #include <limits.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct _item {
  6.   int value;
  7.   struct _item* next;
  8. } Item;
  9.  
  10. void print_list(Item* head);
  11.  
  12. // Aggiunge alla fine
  13. Item* append(Item* head, int value);
  14.  
  15. // Aggiunge all'inizio
  16. Item* prepend(Item* head, int value);
  17.  
  18. // Aggiunge all'indirizzo
  19. Item* insert_at(Item* head, int value, size_t index);
  20.  
  21. // Adds element without ruining sorting property
  22. // Only works for previusly sorted arrays
  23. // Example:
  24. // -> 15
  25. // 1 9 10 (15) 29 32
  26. Item* insert_sorted(Item* head, int value);
  27.  
  28. int main() {
  29.   // ...
  30.   // Insert solution here
  31.   // ...
  32.  
  33.   Item* list = NULL;
  34.   list = insert_at(list, 1, 0);
  35.   print_list(list);
  36.  
  37.   list = insert_at(list, 2, 1);
  38.   print_list(list);
  39.  
  40.   list = insert_at(list, 3, 0);
  41.   print_list(list);
  42.  
  43.   list = insert_at(list, 4, 1);
  44.   print_list(list);
  45.  
  46.   list = insert_at(list, 5, 1);
  47.   print_list(list);
  48.  
  49.   list = insert_at(list, 6, 1);
  50.   print_list(list);
  51.  
  52.   return 0;
  53. }
  54.  
  55. // Aggiunge alla fine
  56. Item* append(Item* head, int value) { return insert_at(head, value, INT_MAX); }
  57.  
  58. // Aggiunge all'inizio
  59. Item* prepend(Item* head, int value) { return insert_at(head, value, 0); }
  60.  
  61. // Aggiunge all'indirizzo
  62. Item* insert_at(Item* head, int value, size_t index) {
  63.   // Se head è null, quindi se non è definita una lista
  64.   if (head == NULL) {
  65.     Item* t_head = (Item*)malloc(sizeof(Item));
  66.     t_head->value = value;
  67.     t_head->next = NULL;
  68.  
  69.     // Returning new head to be replaced
  70.     return t_head;
  71.   }
  72.  
  73.   if (index == 0) {
  74.     Item* t_head = (Item*)malloc(sizeof(Item));
  75.     t_head->value = value;
  76.     t_head->next = head;
  77.  
  78.     // Returning new head to be replaced
  79.     return t_head;
  80.   }
  81.  
  82.   size_t position = 0;
  83.   Item* _iterator = head;
  84.  
  85.   // Finds the position for the item to be inserted
  86.   while (_iterator->next != NULL && ++position < index) {
  87.     _iterator = _iterator->next;
  88.   }
  89.  
  90.   // _iterator -> next == NULL or position == index
  91.   if (_iterator->next == NULL) {
  92.     _iterator->next = (Item*)malloc(sizeof(Item));
  93.     _iterator->next->value = value;
  94.     _iterator->next->next = NULL;
  95.  
  96.     return head;
  97.   }
  98.  
  99.   // _iterator -> next != NULL
  100.   Item* old_next = _iterator->next;
  101.   _iterator->next = (Item*)malloc(sizeof(Item));
  102.   _iterator->next->value = value;
  103.   _iterator->next->next = old_next;
  104.  
  105.   return head;
  106. }
  107.  
  108. void print_list(Item* head) {
  109.   for (Item* _iterator = head; _iterator != NULL; _iterator = _iterator->next) {
  110.     printf("%d -> ", _iterator->value);
  111.   }
  112.  
  113.   printf("NULL\n");
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement