簡體   English   中英

單鏈表中的分段錯誤

[英]segmentation fault in singly linked list

我目前正在編寫一個程序,該程序實現一個鏈表,以按排序順序維護整數列表。 程序從文件中讀取以下格式的整數:

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

“ d”表示從列表中刪除,“ i”插入列表,不允許重復整數。 這是我到目前為止的代碼:

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

當第一個字母為'i'時,它可以正常工作,但是當第一個字母為'd'時,我會遇到分段錯誤。 為什么是這樣?

當您嘗試刪除節點的第一件事時,列表為空,並且在delete功能*head (請注意取消引用!)內部是空指針。

由於*head是空指針,因此ptr也將為空,然后取消引用ptr而不進行檢查。

您可能希望將初始檢查修改為head == NULL || *head == NULL head == NULL || *head == NULL

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

如果首字母為D,則調用delete時您的列表仍然為空。

在您的第一個if ,您需要取消引用指針,如@SomeProgrammerDude所述。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM