简体   繁体   English

单链表中的分段错误

[英]segmentation fault in singly linked list

I am currently writing a program that implements a linked list to maintain a list on integers in sorted order. 我目前正在编写一个程序,该程序实现一个链表,以按排序顺序维护整数列表。 The program reads the integers from a file in the format of 程序从文件中读取以下格式的整数:

d [\t] 7
i [\t] 8
i [\t] 7
i [\t] 10

with 'd' meaning delete from the list and 'i' inserting into the list, with no repeat integers allowed. “ d”表示从列表中删除,“ i”插入列表,不允许重复整数。 This is the code I have so far: 这是我到目前为止的代码:

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

struct Node{
        int data;
        struct Node* next;
};

int delete(struct Node** head, int key){
        if(head == NULL)
        {
                return -1;
        }
        struct Node* ptr = *head;
        struct Node* prev = NULL;

        while(ptr->data != key && ptr->next!=NULL)
        {
                prev = ptr;
                ptr = ptr->next;
        }
        if(ptr->data == key)
        {
                if(prev)
                {
                        prev->next = ptr->next;
                }
                else
                {
                        *head = ptr->next;
                }
                free(ptr);
                return key;
        }
return -1;
}

int insert(struct Node** head, struct Node* newNode){
        struct Node* ptr;
        if(*head == NULL || (*head)->data >= newNode->data)
        {
        newNode->next = *head;
                *head = newNode;
        }
        else
        {
                ptr = *head;
                while(ptr->next != NULL && ptr->next->data < newNode->data)
                {
                        ptr = ptr->next;
                }
                newNode->next = ptr->next;
                ptr->next = newNode;
        }
        return 0;
}

int main(int argc, char* argv[]){
        FILE *fp = fopen(argv[1], "r");
        int num, found;
        char c;
        struct Node* head = NULL;
 struct Node* ptr = head;
        while(fscanf(fp, "%c\t%d\n", &c, &num)!=EOF)
        {
                for(ptr=head; ptr!=NULL; ptr=ptr->next)
                {
                        if(ptr->data == num)
                        {
                                found = 1;
                        }
                }
                if(found != 1)
                {
                        if(c == 'i')
                        {
                                struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
                                newNode->data = num;
                                newNode->next = NULL;
                                insert(&head, newNode);
                        }
                }
                if(c == 'd')
  delete(&head, num);
                }
                found = 0;
        }

        for(ptr=head; ptr!=NULL; ptr=ptr->next)
        {
                printf("%d ", ptr->data);
        }
return 0;
}

It works fine when the first letter is 'i', but whenever the first letter is 'd' I get a segmentation fault. 当第一个字母为'i'时,它可以正常工作,但是当第一个字母为'd'时,我会遇到分段错误。 Why is this? 为什么是这样?

When you try to delete a node first thing you do, the list is empty and inside the delete function *head (notice the dereference!) is a null pointer. 当您尝试删除节点的第一件事时,列表为空,并且在delete功能*head (请注意取消引用!)内部是空指针。

Since *head is a null pointer, so will ptr be, and you then dereference ptr without checking for that. 由于*head是空指针,因此ptr也将为空,然后取消引用ptr而不进行检查。

You might want to modify the initial check to be head == NULL || *head == NULL 您可能希望将初始检查修改为head == NULL || *head == NULL head == NULL || *head == NULL . head == NULL || *head == NULL

whenever the first letter is 'd' I get a segmentation fault.

If the first letter is D, your list is still empty when you call delete . 如果首字母为D,则调用delete时您的列表仍然为空。

In your first if , you need to dereference your pointer, as @SomeProgrammerDude mentionned. 在您的第一个if ,您需要取消引用指针,如@SomeProgrammerDude所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM