【问题标题】:Why does this linked-list ordered insert segfault?为什么这个链表有序插入段错误?
【发布时间】:2013-04-23 04:47:33
【问题描述】:

我正在开发一个按排序顺序插入链接列表的程序,但它不断出现段错误,我不知道为什么。我怀疑它与指针有关,但我不能说,因为在我的编程生涯的这一点上,这些对我来说仍然有点混乱。另外,我必须保持插入原型相同。我无法将节点参数更改为双指针。谢谢!

#include <stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include<conio.h>

typedef struct node {
    int data;
    struct node  *next;
};

int main ()
{
    struct node* first;  
    int temp,x,y;
    struct node *create (struct node *first);
    first = NULL;     
    void display (struct node *first);
    printf ("\n\nCreating a Linked List\n");
    printf ("\nEnter Element: ");
    scanf ("%d", &x);
    y=x;
    while(y>0)
    {
        scanf ("%d", &x);
        insert_sorted_linked_list(first,x); 
        y--;
    }

    printf ("\nThe list after creation is: ");
    display (first);

    printf ("\nThe sorted list is: ");
    display (first);
    return(0);
}   /*END OF MAIN*/

insert_sorted_linked_list(struct node* head, int val)
{
    struct node* pCur;
    struct node* pNew = (struct node*) (malloc(sizeof(struct node)));

    pNew -> data = val;
    pNew ->next = NULL;
    pCur = head;

    if( pCur->data == NULL )
    {
        head->data = pNew->data;
        head->next = NULL;
    }
    else if (pNew->data  <   pCur->data)
    {
        pNew ->next = pCur ;
        head = pNew;
    }
}

void display (struct node *first)
{   struct node *save;         /*OR sort *save     */
    if (first == NULL)
        printf ("\nList is empty");
    else
    {  save = first;
        while (save != NULL)
        {   printf ("-> %d ", save->data);
            save = save->next;
        }
        getch();
    }
    return;
}   

编辑:将 main 更改为 int。调试器不喜欢这些行:

struct node* pNew = (struct node*) (malloc(sizeof(struct node)));

如果(pCur->数据 == NULL)

但不确定是什么问题。

编辑 2:

我决定在明天早上之前我不会按照他要求的原始方式让它工作,所以我在这里发布了一个修改版本。那个没有段错误,但事实证明也有逻辑错误。

#include <stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include<conio.h>

typedef struct s
{
    int data;
    struct s  *next;
}node;


void insert_sorted_linked_list(node **head, int val);
void display (node **first);
void freeList(node **first);

int main ()
{
    node* first;
    int x,y;

    first = NULL;

    printf ("\n\nCreating a Linked List\n");
    printf ("\nEnter number of elements: ");
    scanf ("%d", &x);
    y=x;
    while(y>0)
    {
        scanf ("%d", &x);
        insert_sorted_linked_list(&first,x);
        y--;
    }
    printf ("\nThe sorted list is: ");
    display (&first);
    freeList(&first);
    return 0;
}

void insert_sorted_linked_list(node **head, int val)
{
    node* pCur;
    node* pNew = (node*) (malloc(sizeof(node)));
    pNew->data = val;
    pNew->next = NULL;
    pCur = (*head);

    if( pCur == NULL )
    {
        (*head) = pNew;
    }
    else if(pNew->data  <   pCur->data)
    {
        pNew->next = pCur;
        (*head) = pNew;
    }
    else
    {
    while(pCur->next!=NULL && pNew->data  >  pCur->next->data)
        pCur = pCur->next;
    pNew->next = pCur->next;
    pCur->next = pNew;
    }
}


void display (node **first)
{
    node *lisprint;         /*OR sort *lisprint     */
    if (*first == NULL)
    printf ("\nList is empty");
    else
    {
        lisprint = *first;
        while (lisprint != NULL)
        {
            printf ("-> %d ", lisprint->data);
            lisprint = lisprint->next;
        }
        getch();
    }
}   /*END OF FUNCTION DISPLAY*/

void freeList(node **first)
{
    node *i;
    i = *first;
    while(i !=NULL)
    {
        (*first) = (*first)->next;
        free(i);
        i = *first;
    }
}

谢谢!

【问题讨论】:

  • 我认为格式本身足以解决段错误。
  • 学习使用调试器。如果您在 Linux 上,请尝试 GDB。它会告诉你发生段错误的确切行。
  • int main()。 A-void void.
  • 您的主要错误出现在插入函数中的 19 行代码之间,这是取消引用 NULL 指针的访问冲突。我可以说是哪一个以及为什么,但我已经比你付出了更多的努力。
  • "我必须保持插入原型不变。我不能将节点参数更改为双指针。谢谢!" 我可以吗?说真的,如果你不能改变原型,包括返回类型(这在严格的 C99 中甚至是不合法的,因为它需要声明的返回类型,你的被假定为 int 和甚至从未指定),那么除了通过外部变量(读取:全局)之外,没有办法更改或发送头指针。既然您已经知道这一切,并且可能已经拒绝了其他更改原型的尝试(或者您不会提出它),那么究竟有什么可以帮助您?

标签: c linked-list segmentation-fault


【解决方案1】:

请正确格式化您的代码!只是为了看看错误在哪里,真是痛苦! 事实上,

/*PROGRAM TO CREATE & THEN DISPLAY THE LINKED LIST IN SORTED FORM*/
#include <stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include<conio.h>

typedef struct s
{
    int data;
    struct s  *next;
}node; 
/* your declaration for typedef was incorrect. We use typedef in C for structures so   that we do not have to repeat struct s everytime. Using typedef, we can write node as we do in c++ */

void insert_sorted_linked_list(node **head, int val); /* do not need to return anything, as well as see the parameter. When we want to change a pointer, we pass the address to the pointer, as it results in passing by value */
void display (node **first); /* same here and below */
void freeList(node **first); /* if you don't do this, memory leak!!! */

int main ()
{
    node* first;  /*OR sort *first,*list,*pass  */
    int temp,x,y;

    first = NULL;      /*OR sort *create()  */

    printf ("\n\nCreating a Linked List\n");
    printf ("\nEnter number of elements: "); /* specify what you want the user to enter */
    scanf ("%d", &x);
    y=x;
    while(y>0)
    {
        scanf ("%d", &x);
        insert_sorted_linked_list(&first,x); /*CALLING CREATE FUNCTION, notice the &first*/
        y--;
    }
    printf ("\nThe list after creation is: ");
    display (&first);
    printf ("\nThe sorted list is: ");
    display (&first);
    freeList(&first);
    return 0;
}   /*END OF MAIN*/

void insert_sorted_linked_list(node **head, int val)
{
    node* pCur;
    node* pNew = (node*) (malloc(sizeof(node)));
    pNew->data = val;
    pNew->next = NULL;
    pCur = (*head);

    if( pCur == NULL )
    {
        (*head) = pNew;
    }
    else if (pNew->data  <   pCur->data)
    {
        pNew->next = pCur ;
        (*head) = pNew;
    }
}

/*DISPLAY FUNCTION*/
void display (node **first)
{
    node *save;         /*OR sort *save     */
    if (*first == NULL)
    printf ("\nList is empty");
    else
    {
        save = *first;
        while (save != NULL)
        {
            printf ("-> %d ", save->data);
            save = save->next;
        }
        getch();
    }
}   /*END OF FUNCTION DISPLAY*/

void freeList(node **first)
{
    node *i;
    i = *first;
    while(i !=NULL)
    {
        (*first) = (*first)->next;
        free(i);
        i = *first;
    }
}

【讨论】:

  • 虽然可爱,但 OP 已经声明不允许修改 insert_sorted_linked_list() 的接口:" 我必须保持插入原型相同。我无法更改节点参数转换为双指针。”
  • 啊,现在才看到!那么,愿丹尼斯·里奇帮助他!
  • 是的,这不是我的选择。当我在网上查看时,我看到很多人都这样做,但我的教授指定我必须使用那个原型。如果这只是对他来说很有趣,或者是什么。抱歉格式化,我将不得不处理这个问题。现在可能特别糟糕,因为我一直在拆开它并重建。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多