title: C - Deallocating a list of integers
body: <p>We have to use the following declaration of a list:</p>
  
  <pre><code>typedef struct node {
      int val;
      struct node* next;
  } node;
  </code></pre>
  
  <p>and write these functions:<br>
  1) <code>node* add(int val, node* list)</code> Inserts a new node at the beginning of the given list and returns it's pointer.<br>
  2) <code>print(node* list)</code> Prints the elements of the list.<br>
  3) <code>delete(node* list)</code> Deallocates all the elements of the given list<br>
  I'm not sure if I am correctly using the memory, but the code I wrote give a weird output :/
  Can someone help me out ? Thanks</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct node {
      int val;
      struct node* next;
  } node;
  
  node* aux;
  
  node* add(int n, node* list){
     aux = malloc(sizeof(node));
     if(list==NULL){
        aux-&gt;val=n;
        aux-&gt;next=NULL;
        }
      else{
        aux-&gt;val=n;
        aux-&gt;next=list;
     }
     return aux;
  }
  
  
  void print(node* list){
     printf("[");
     while(list != NULL){
     if(list-&gt;next!=NULL)printf("%d, ",list-&gt;elem);
     else printf("%d",list-&gt;elem);
     list = list-&gt;next;
     }
  printf("]\n");
  }
  
  void delete(node* list){
     list=NULL;
     //printf(list);  prints the expected output: []
  }
  
  /*EDIT: Correct function delete
  void delete(node* list){
     while( list != null){
     node* temp = list-&gt;next;
     free(list);
     list=temp;
     }
  }
  */ 
  
  int main(){
     node* x = add(5, add(3, add(2, add(7, add(4, add(1, add(6, NULL)))))));
     print(x);
     delete(x);
     print(x); //still prints the initial list 
     return(0);
  }
  </code></pre>