title: Linked list QuickSort C
body: <p>I wrote some simple code that sorts a list that contains passwords, and frequency of usage of this password. The problem is, after reading the text file, the nodes are not connecting together. I can't understand why. It keeps only the last value.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;assert.h&gt;
  
  
  typedef struct list_element{
      char passwort[100];
      int haufigkeit;
      struct list_element *next;
  } list_element;
  
  typedef struct list{
  
      list_element *first;
      list_element *last;
  } list;
  
  
  void init_list(list* mylist)
  {
      mylist-&gt;first=NULL;
      mylist-&gt;last=NULL;
  }
  
  
  // Diese Funktion fügt Listenelemente am Anfang der Liste an
  void insert_front(list_element* le, list* mylist)
  {
      if(mylist-&gt;first == NULL){
          le-&gt;next = mylist-&gt; first;
          mylist-&gt;first=le;
          mylist-&gt;last=le;
  
          printf("%s %d \n",le-&gt;passwort, le-&gt;haufigkeit);
  
      }
      else {
          le-&gt;next = mylist-&gt; first;
          mylist-&gt;first= le;
  
      }
  
      // printf("%s %d \n",le-&gt;passwort, &amp;le-&gt;haufigkeit);
  
  }
  
  // Speicher für Listenelemente wieder freigeben
  void free_list(list* mylist)
  {
      free((mylist)-&gt;first);
      free(mylist);
      mylist=NULL;
  }
  
  
  // Namen, Zahlen Paare in Liste einlesen
  void read_data(char* filename, list* mylist)
  {
      assert(mylist != NULL);
      FILE* f=fopen(filename,"rb");
      assert(f != NULL);
      list_element* temp = malloc(sizeof(list_element));// * Speicher allozieren
  
      while (fscanf(f,"%s %d",temp-&gt;passwort, &amp;temp-&gt; haufigkeit) != EOF)
      {
  
          fscanf(f,"%s %d",temp-&gt;passwort, &amp;temp-&gt; haufigkeit);// * Daten in list_element einlesen
          printf("%s %d \n",temp-&gt;passwort, temp-&gt;haufigkeit);
  
          insert_front(temp, mylist); // * insert_front benutzen um list_element in Liste einzufügen
  
      }
      fclose(f);
  }
  
  // Pivot finden, das die Liste aufteilt
  list_element* partition( list* input, list* left, list* right ){
      list_element* pivot= input-&gt;first;
      printf("hi");
      list_element *i;
      for(i=input-&gt;first; i != NULL; i=i-&gt;next){
  
          if ((i -&gt; haufigkeit) &lt; (pivot -&gt; haufigkeit)){
              insert_front( i, left);
          }
          else{
              insert_front( i, right);
          }
  
      }
  
      return pivot;
  }
  
  
  // Hauptfunktion des quicksort Algorithmus
  void qsort_list(list* mylist){
      // HIER Code einfügen
      //  list liste= mylist;
      list right;
      list left;
      init_list(&amp;right);
      init_list(&amp;left);
      list_element* pivot;
      printf("hi11");
      if (mylist-&gt;last != mylist-&gt;first){
          printf("d1 \n");
  
  
          pivot = partition(mylist, &amp;left, &amp;right );
          printf("pivot %s %d \n",pivot-&gt;passwort, pivot-&gt;haufigkeit);
  
          qsort_list(&amp;left);
          qsort_list(&amp;right);
          /*
           if(left.first == NULL){
           pivot-&gt;next = right.first;
           mylist-&gt;first = pivot;
           mylist-&gt;last = right.last;
           }
           else if(right.first == NULL){
           left.last-&gt; next = pivot;
           mylist-&gt;first = left.first;
           mylist-&gt;last = pivot;
           }
           else{
           left.last-&gt;next=pivot;
           pivot-&gt;next = right.first;
           mylist-&gt;first = left.first;
           mylist-&gt;last = right.last;
           }
  
           printf("pivot %s %d \n",pivot-&gt;passwort, pivot-&gt;haufigkeit);
           }
           /* mylist-&gt;first = left-&gt; first;
           mylist-&gt;last=right-&gt;last;
           pivot-&gt;next=right-&gt;last;
           left.last-&gt;next=pivot;
           pivot-&gt;next=right.first;
           */
          if(left.first == NULL) {
              // Special
  
              left.first = pivot;
              mylist-&gt;first = left.first;
          } else {
              // REGULAR
              mylist-&gt;first = left.first;
              left.last-&gt;next = pivot;
          }
          if(right.first == NULL) {
              // Special
              pivot-&gt;next = right.first;
              mylist-&gt;last = pivot;
          } else {
              // Regular
              pivot-&gt;next = right.first;
              mylist-&gt;last = right.last;
          }
  
  
  
      }
  }
  
  // Liste ausgeben
  void print_list(list* mylist){
  
      list_element* current = mylist-&gt; first;
      while(current != NULL){
          printf("%s %d \n",current-&gt;passwort,current-&gt;haufigkeit);
          current=current-&gt;next;
      }
  }
  
  // Argumente einlesen, Liste kreieren, verarbeiten und ausgeben
  int main(int argc, char** args)
  
  {
      if (argc != 2)
      {
          printf("Nutzung: %s &lt;Dateiname&gt;\n",args[0]);
          return 1;
      }
      list mylist;
      init_list(&amp;mylist);
      read_data(args[1],&amp;mylist);
      qsort_list(&amp;mylist);
      printf("Sortierte Liste:\n");
      print_list(&amp;mylist);
      free_list(&amp;mylist);
      return 0;
  }
  </code></pre>
  
  <p>the list txt is in form  'password frequency' like </p>
  
  <pre><code>daniel 27720 
  welcome 22204 
  adobeadobe 27840 
  superman 24499 
  7777777 19818 
  liverpool 18008 
  princess 28132 
  1qaz2wsx 22180 
  </code></pre>