For a class I have to use a recursive function to quicksort a linked list. This is on Lunix. This is my quicksort function, but it is causing a Segmentation fault. After some googling that means I have a pointer pointing to something it isn't allowed to, but I am not sure what I am doing wrong. I am not a new programmer, but I just started C a month ago so pointers are still a little confusing to me. Here is the function:
#include <stdlib.h>
#include <stdio.h>
#include "node.h"
//quicksort method
//takes one node pointer and recursively sorts in ascending order, returning the head node
struct mynode* quicksort(struct mynode *head)
{
int pivot = head->value;
struct mynode *current, *left, *right, *l_current, *r_current;
left = (struct mynode *)malloc(sizeof(struct mynode));
right = (struct mynode *)malloc(sizeof(struct mynode));
l_current = left;
r_current = right;
for (current=head; current; current=current->next) {
if (current->value < pivot) {
l_current->value = current->value;
l_current->next = (struct mynode *)malloc(sizeof(struct mynode));
l_current = l_current->next;
} else {
r_current->value = current->value;
r_current->next = (struct mynode *)malloc(sizeof(struct mynode));
r_current = r_current->next;
}
}
left = quicksort(left);
right = quicksort(right);
for (current=left; current; current=current->next) {
}
current->next = right;
return left;
}
Thanks for the help.