无法找到发生分段错误的位置

时间:2015-02-04 22:28:17

标签: c segmentation-fault quicksort

对于一个类,我必须使用递归函数来快速排序链表。这是在Lunix上。这是我的快速排序功能,但它导致了分段错误。经过一些谷歌搜索,这意味着我有一个指针指向不允许的东西,但我不知道我做错了什么。我不是一个新的程序员,但我一个月前刚开始使用C,所以指针对我来说仍然有些混乱。这是功能:

#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;
}

感谢您的帮助。

0 个答案:

没有答案