title: Radix sort using array of linked list as bin in C
body: <p>I am trying to implement radix sort in C using an array of linked lists as a bin for storing the elements according to their place value. But the heads of the linked lists are always staying <code>NULL</code>. And if I try to access them directly without the temp variable, it is giving segmentation fault. Please help.
  Thank you in advance.</p>
  <pre><code>struct node {
      int data;
      struct node *next;
  };
  
  // array of linked list
  struct node *radix[10];
  
  // initializing all list head with null
  void init() {
      int i;
      for (i = 0; i &lt; 10; i++) {
          radix[i] = NULL;
      }
  }
  
  // fucntion for making a node
  struct node *make_new_node(int data) {
      struct node *new_node = (struct node *)malloc(sizeof(struct node));
      new_node-&gt;next = NULL;
      new_node-&gt;data = data;
      return new_node;
  }
  
  // function for getting the maximum digit
  int get_max_digit(int *arr, int n) {
      int i, max = arr[0], count = 0;
      // This loop is for finding the maximum number
      for (i = 0; i &lt; n; i++) {
          if (arr[i] &gt; max) {
              max = arr[i];
          }
      }
      // This loop is for getting the total number digits of the maximum number
      while (max != 0) {
          max /= 10;
          count++;
      }
      return count;
  }
  
  // function for inserting the digit in the last node
  void insert_into_bin(int data, int rad) {
      struct node *new_node = make_new_node(data);
      struct node *temp = radix[rad];
      if (temp == NULL) {
          temp = new_node;
      } else {
          while (temp-&gt;next!=NULL) {
              temp = temp-&gt;next;
          }
          temp-&gt;next = new_node;
      }
  }
  
  // function for radix sort
  void radix_sort(int *arr, int n) {
      int i, j = 0, k, pass, digit, div = 1;
      pass = get_max_digit(arr, n);
      
      for (i = 1; i &lt;= pass; i++) {
          printf(&quot;\ndiv = %d\n\n&quot;, div);
          for (j = 0; j &lt; n; j++) {
              // getting the corresponding digit
              digit = (arr[j] / div) % 10;
              
              // inserting into the bin
              insert_into_bin(arr[j], digit);
          }
          // now multiplying div with 10 and storing it in div
          div *= 10;
          // Now the list is sorted in the array of linked list
          // Time to retrieve them
          j = 0;
          for (k = 0; k &lt; 10; k++) {
              struct node *temp = radix[i];
              while (temp != NULL) {
                  arr[j++] = temp-&gt;data;
              }
          }
          init();
      }
  }
  
  // function for printing the array
  void display(int *arr, int n) {
      int i;
      for (i = 0; i &lt; n; i++) {
          printf(&quot;%d &quot;, arr[i]);
      }
      printf(&quot;\n&quot;);
  }
  
  // driver function
  int main() {
      // calling the init function
      init();
      int arr[6] = { 655, 12, 7845, 2, 45, 45122 };
      radix_sort(arr, 6);
      display(arr, 6);
      return 0;
  }
  </code></pre>