0

我正在学习如何在C中构建链表。我的程序可以编译,但由于某种原因我无法弄清楚,我遇到了分段错误。我一直在试图找出问题所在,但我没有任何运气。这是错误的代码:

int len()
{
    struct list * current = head;
    int length = 0; 

    while (current != NULL)
    {
        length++;
        current = current -> next; //move to next node
    }
    return length; 
}


struct list * search ( int key)
{
    struct list * current = head;

    while (current != NULL && current->data != key)
        current = current -> next;

    if (current != NULL && current -> data == key)
        return current;
    return NULL;
}



/* Insert a new data element with key d into the end of the list. */
void insert(int d )  //  at the end
{
    struct list * current = head; 
    struct list * new;
    while (current -> next != NULL)
        current = current -> next;
    new = (struct list *)malloc(sizeof(struct list));
    new -> data = d; 
    current -> next = new;
    new -> next = NULL;     
}


void insertAfter(int d, int where )  //  insert at the middle
{
    struct list * marker = head;
    struct list * new;

    while(marker -> data != where)
        marker = marker -> next;
    new = (struct list*)malloc(sizeof(struct list));

    new -> next = marker -> next; 
    marker -> next = new;
    new -> data = d; 
}


/* Remove the node with value d from the list */
/* assume no duplicated keys in the list */
/* If the list is empty, call prtError() to display an error message and return -1. */

void delete(int d)
{
    struct list * current1 = head; 
    struct list * current2;

    if (len() == 0)
    { //prtError("empty");
        exit(0);
    }
    if (head -> data == d)
    { 
        head = head -> next;
    }

    //Check if last node contains element
    while (current1->next->next != NULL)
        current1 = current1->next;

    if(current1->next->data == d)
            current1->next == NULL; 


    current1 = head; //move current1 back to front */

    while(current1 -> next -> data != d)
        current1 = current1 -> next; 

    current2 = current1 -> next;
    current1 -> next = current2 -> next; 

}

我在该行的删除方法中遇到分段错误:

while(current1 -> next -> data != d)

为什么这是错误的?

4

5 回答 5

0

您发布的代码存在许多问题,但您在评论中提到您担心insert()head它崩溃的原因是,如果调用时为 NULL ,您将取消引用 NULL 指针insert()

head当它为 NULL 时,您将需要一个特殊情况来插入:

if (head) {
    while (current -> next != NULL)
        current = current -> next;
}
new = (struct list *)malloc(sizeof(struct list));
new -> data = d;
if (current) {
    current -> next = new;
} else {
    head = new;
}
new -> next = NULL;

您应该检查其他功能中是否存在类似问题。使用您的search()函数作为避免在循环中取消引用 NULL 指针的示例。

于 2013-07-09T21:41:07.463 回答
0

你有几个问题,在insert

while (current -> next != NULL)

你没有检查 if currentis NULL。有类似的问题delete

if (head -> data == d)

你需要在head这里检查并且:

while (current1->next->next != NULL)

也是一个问题。

于 2013-07-09T21:42:00.463 回答
0

也许它在insertAfter

while(marker -> data != where)
    marker = marker -> next;

data == where如果没有找到带有的节点,marker将是一个NULL指针。
稍后在您的代码中取消引用该NULL指针:

new = marker -> next;

它会产生段错误。检查是否marker->next != NULL应该避免这种情况:

while(marker->next != NULL && marker -> data != where)
    marker = marker -> next;

但是您应该尝试使用调试符号(-g选项)编译您的程序,并在 GDB 等调试器中逐行运行它。

于 2013-07-09T21:58:20.093 回答
0

插入之后有一个错误,因为您将超出列表的末尾找不到元素。

于 2013-07-09T21:43:41.820 回答
0

insert,

while(current->next != NULL) current = current->next;

这将保证current == NULL.

current->next = new;

每次都会崩溃。

于 2013-07-09T21:47:17.480 回答