尝试实现LinkedList时C分段错误

时间:2013-04-06 12:39:07

标签: c pointers segmentation-fault

我已获得以下代码:

#include <stdio.h>
#include <stdlib.h>
#define MAXN 100
typedef int key;
typedef int data;
struct list * createElement(key k, data info);

struct list{
    key k;
    data info;
    struct list *next;
};
struct list *L;
void init(key k, data info)
{
    L = createElement(k, info);
}
struct list * createElement(key k, data info)
{
    struct list *temp;
    temp = (struct list *) malloc(sizeof(*temp));
    temp->k = k;
    temp->info = info;
    return temp;
}
void insert(struct list * element)
{
    element->next = L;
    L = element;
}
void insertBefore(struct list * element, key k)
{
    struct list * currentElement = L;
    while(currentElement != NULL)
    {
        if(currentElement->k == k)
        {
            struct list *temp = currentElement;
            currentElement = element;
            currentElement->next = temp;
            return;
        }
        currentElement = currentElement->next;
    }

}
void insertAfter(struct list * element, key k)
{
    struct list * currentElement = L;
    while(currentElement != NULL)
    {
        if(currentElement->k == k)
        {
            struct list *temp = currentElement->next;
            currentElement->next = element;
            element->next = temp;
            return;
        }
        currentElement = currentElement->next;
    }
}
void deleteElement(struct list * element)
{
    struct list * currentElement = L;
    while(currentElement != NULL)
    {
        if(currentElement == element)
        {
            struct list * temp = currentElement;
            currentElement = currentElement->next;
            free(temp);
            return;
        }
        currentElement = currentElement->next;
    }
}
struct list * getElementByKey(key k) {
    printf("\n1");
    struct list *currentElement = L;
    printf("2");
    while(currentElement != NULL)
    {
        printf("3");
        if(currentElement->k == k)
        {
            printf("4");
            return currentElement;
        }
        printf("5");
        currentElement = currentElement->next;
        printf("6");
    }
    printf("There is no such element in the list");
}
struct list * pop()
{
    struct list *element = L;
    L = L->next;
    return element;
}
int main()
{
    init(0, 13);
    struct list * element = createElement(5, 155);
    insert(element);
    struct list * k = createElement(7, 243);
    insert(k);
    //insertBefore(createElement(3, 100), 5);
    printf("The first element value is: %d", pop()->info);
    printf("The second element value is: %d", pop()->info);
    printf("The  element value is: %d", getElementByKey(5)->info);

    return 0;
}

因此,当我执行它时,调试器在第84行给出了segmantation fault,它在方法getElementByKey上为if(currentElement->k == k)。我知道我试图访问一个不存在的元素(因为我使用pop方法,它删除了列表中的第一个元素),但它应该打印一条警告消息。似乎前一个元素的密钥存在问题,或者我没注意到的东西。

3 个答案:

答案 0 :(得分:3)

createElement函数中,您忘记将next指针初始化为NULL。

struct list * createElement(key k, data info)
{
    struct list *temp;
    temp = (struct list *) malloc(sizeof(*temp));
    temp->k = k;
    temp->info = info;
    temp->next = NULL;
    return temp;
}

因此,当您浏览列表时,它永远不会找到列表的最后一个元素,因为下一个指针永远不会等于NULL,并且会进入内存中的某个随机位置,尝试访问不允许的部分这样做。

答案 1 :(得分:1)

删除元素时(使用deleteElement()),您将错过重新初始化L


if(currentElement->k == k)中的段落违规最常见的是currentElement指的是无效的内存地址。当您在第84行之前测试是否针对NULL某些行时,只能是currentElement明确地分配了无效地址或者它已指向的地址已被释放的情况。

deleteElement()中显示的代码我假设后者。

答案 2 :(得分:1)

两个问题

1)在createElement()

需要

temp->next = NULL;

2)在insertBefore()

不更改列表的指针

currentElement = element;

e.g)

void insertBefore(struct list * element, key k)
{
    struct list * currentElement = L; //add case of L->k
    while(currentElement != NULL)
    {
        if(currentElement->next && currentElement->next->k == k)
        {
            element->next = currentElement->next;
            currentElement->next = element;
            return;
        }
        currentElement = currentElement->next;
    }
}