首页 文章

为什么这个链表有序插入段错误?

提问于
浏览
-3

我正在研究一个按排序顺序插入链接列表的程序,但它会保留seg错误,我无法弄清楚原因 . 我怀疑它与指针有关,但我不能说,因为在我的编程生涯中,这些对我来说仍然有点混乱 . 另外,我必须保持插入原型相同 . 我无法将节点参数更改为双指针 . 谢谢!

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

if(pCur-> data == NULL)

不知道有什么问题 .

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

谢谢!

1 回答

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

相关问题