0

我对 c (和这个站点)相当陌生,并且我在分段错误方面遇到了很多问题。我正在编写一个程序,该程序创建一个数字链接列表并按升序插入值。

     void insert(struct element **head, struct element *new){   
            if((*head)->next == NULL && (*new).i > (*(*head)->next).i){
                (*head)->next = new;
                return;     
            }
            if((*head)->next == NULL && (*new).i < (*(*head)->next).i){
                new->next = (*head)->next;
                *head = new;    
                return;
            }
            struct element *prev = *head;
            struct element *current = (*head)->next;
            while(current->next != NULL){
                if((*new).i < (*current).i){
                    prev = current;
                    current = current->next;
                } else if((*new).i > (*current).i){
                    new->next = current;
                    prev->next = new;
                }
            }
        }
        int main (void){
            struct element **head;
            int value;
            printf("%s", "TEST" );
            printf("%s" , "Please type in an integer value. ");
            scanf("%d" , &value);
            printf("%s", "TEST" );
            do{
                printf("%s", "TEST" );
                struct element *new;
                if((new = malloc(sizeof(struct element))) == NULL){
                return(NULL);
                }
                printf("%s", "TEST" );
                (*new).i = value;
                printf("%s", "TEST" );
                if(head == NULL){
                    *head = new;
                    printList(*head);
                }  else if(value <= 0){
                    printListBackwards(*head);
                }   
                else {

                    insert(head, new);
                    printList(*head);
                }
                } while(value > 0);

我不需要关于插入或任何东西的逻辑是否正确的帮助。我什至没有机会真正测试它,因为在提示符后输入整数后,我立即遇到分段错误。我知道这看起来很时髦,但规范要求您使用指向结构的指针(链表的头部)的指针。

4

3 回答 3

2

您确定要 head 成为 anelement**而不是 anelement*吗?这种额外的分离程度会给您带来问题,其中最重要的是难以阅读的代码。

这是让我跳出来的主要内容:

if(head == NULL){
    *head = new;
    printList(*head);
}

您正在确认 head 是一个 NULL 指针,然后立即尝试使用*. 如果你真的坚持 head 是一个双指针,那么你需要在取消引用之前动态分配它。像这样:

if(head == NULL){
    head = malloc(sizeof(element*));
    *head = new;
    printList(*head);
}

这实际上可能在语法上并不完美(我来自 C++),但你明白了。不过,说到 C++,在 C 中将变量命名为“new”通常被认为是不好的做法,因为new它是 C++ 中的关键字。

于 2013-04-05T01:55:20.353 回答
0
struct element **head;

你不想要那个。反而,

struct element *head = NULL;

然后,当你调用插入时,使用

insert(&head, new);

您还有许多其他错误和不良用法,但这是您特定问题的开始。

于 2013-04-05T02:06:13.983 回答
0

您的帖子的第二行出现了段错误

if((*head)->next == NULL && (*new).i > (*(*head)->next).i){
    (*head)->next = new;
    return;     
}

分段错误意味着您正在尝试访问不允许访问的内存。例如,您不能取消引用 NULL 指针。

你的if陈述是这样评估的。检查是否(*head)->next为空。

如果它不为 NULL,则跳过其余部分。

如果它为 NULL,那么您可以将以下每个替换(*head)->nextNULL. 这意味着以下部分&& (*new).i > (*(*head)->next.i)可以重写如下&& (*new).i > ((*NULL).i)......

简而言之,您正在尝试取消引用 NULL 指针值。

另请参阅@Parker Kemp 的帖子。有很多次你正确地检查了 NULL 但误解了它的含义。

我可以为你重写代码,但我认为你会从学习教程或本教程中受益更多

我强烈建议您绘制数据结构图并为指针绘制箭头。

于 2013-04-05T01:45:04.300 回答