Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.10 KB | None | 0 0
  1. /**
  2.  *  Source file for a linked list in C
  3.  *
  4.  * @authors:        Michael Denzel
  5.  * @creation_date:  2016-09-05
  6.  * @contact:        m.denzel@cs.bham.ac.uk
  7.  */
  8.  
  9. //standard includes
  10.  
  11. // TODO: Add the includes you need
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. //own includes
  15. #include "linkedlist.h"
  16.  
  17.  
  18. // TODO: Implement those methods!
  19. int get(list * l, unsigned int index){
  20.     if(index > 0)
  21.     {
  22.         return get(l->next, index-1);
  23.     }
  24.        
  25.     return l->value;   
  26. }
  27.  
  28. int prepend(list * l, int data){
  29.     list* preElement = malloc(sizeof(list));
  30.     init(preElement);
  31.  
  32.     if(preElement == NULL)
  33.     {
  34.         return -1;
  35.     }
  36.  
  37.     preElement->value = data;
  38.     preElement->next = l;
  39.  
  40.     return 0;
  41. }
  42.  
  43. int append(list * l, int data){
  44.     if(l->next != NULL)
  45.     {
  46.         return append(l->next, data);
  47.     }
  48.    
  49.     list* postElement = malloc(sizeof(list));
  50.     init(postElement);
  51.        
  52.     if(postElement == NULL)
  53.     {
  54.         return -1;
  55.     }
  56.  
  57.     postElement->value = data;
  58.     l->next = postElement;
  59.  
  60.     return 0;
  61. }
  62.  
  63. int remove_element(list * l, unsigned int index){
  64.     if(index > 1)
  65.     {
  66.         if(l->next == NULL)
  67.         {
  68.             return -1;
  69.         }
  70.  
  71.         return remove_element(l->next, index - 1);
  72.     }else if(index == 1)
  73.     {
  74.         if(l->next == NULL)
  75.         {
  76.             return -1;
  77.         }
  78.  
  79.         list* tail = l->next->next;
  80.  
  81.         l->next = tail;
  82.        
  83.         free(l->next);
  84.     }else
  85.     {
  86.         free(l);       
  87.     }
  88.  
  89.     return 0;  
  90. }
  91.  
  92. int insert(list * l, unsigned int index, int data){
  93.    
  94.     if(index > 1)
  95.     {
  96.         if(l->next == NULL)
  97.         {
  98.             return -1;
  99.         }
  100.  
  101.         return insert(l->next, index - 1, data);
  102.     }else if(index == 1)
  103.     {
  104.         list* newElement = malloc(sizeof(list));
  105.         init(newElement);
  106.         newElement->value = data;
  107.  
  108.         if(l->next == NULL)
  109.         {
  110.             l->next = newElement;
  111.         }else
  112.         {
  113.             list* tail = l->next;
  114.             l->next = newElement;
  115.             newElement->next = tail;
  116.         }
  117.     }else
  118.     {
  119.         prepend(l, data);
  120.     }
  121.  
  122.     return 0;
  123. }
  124.  
  125. void print_list(list * l){
  126.     printf("%d", l->value);
  127.  
  128.     if(l->next != NULL)
  129.     {
  130.         printf(", ");
  131.         print_list(l->next);
  132.     }
  133. }
  134.  
  135. void init(list * l){
  136.     l->next = malloc(sizeof(list*));
  137.     l->value = 0;
  138. }
  139.  
  140. void destroy(list *l){
  141.     if(l->next != NULL)
  142.     {
  143.         destroy(l->next);
  144.     }
  145.    
  146.     free(l);
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement