简体   繁体   English

链表上的 EXC_BAD_ACCESS (C)

[英]EXC_BAD_ACCESS on Linked List (C)

i'm having trouble with my program.我的程序有问题。 I implemented a linked list (which i'm sure is working) but i'm having problem using it... it seems like it can read a certain amount of nodes and then it breaks and stops, i get an error in output debugging: EXC_BAD_ACCESS (code=2, address=0x436c6c6441) referring to this line if (strcmp(temp->data, word) == 0) in "Search" function.我实现了一个链接列表(我确定它正在工作)但我在使用它时遇到问题......它似乎可以读取一定数量的节点然后它会中断并停止,我在 output 调试中遇到错误: EXC_BAD_ACCESS (code=2, address=0x436c6c6441) ) 在“搜索”function 中引用此行if (strcmp(temp->data, word) == 0) Output seems correct until it breaks (during the print function, it does not print the whole list) Output 在中断之前似乎是正确的(在打印 function 期间,它不会打印整个列表)

Here's the code:这是代码:

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

struct node{
    char* data;
    struct node* next;
};

int Search(struct node** head, char* word);
void Print(struct node* head);
void Insert(struct node** head, char* word);
void Delete_Node(struct node** head, char* word);


int main() {
    static struct node *head = NULL;

    char word[10];
    int temp;
    int check;

    for (temp = 0; temp < 300; temp++) {
        temp = scanf("%s", word);
        Insert(&head, word);
    }

    char command[10];
    char print[6] = "Print";
    char delete[7] = "Delete";

    for (;;) {
        // Command or word to search for in the list
        scanf("%s", command);
        
        if (strcmp(command, print) == 0) {
            Print(head);
        }

        if (strcmp(command, delete) == 0){
            char todelete[15];
            scanf("%s",todelete);
            Delete_Node(&head,todelete);
        }

        else { 
            check = Search(&head, command);
            if (check == 0) {
                printf("error\n");
                return 1;
            }
            else
                printf("found\n");
        }
    }

}


int Search(struct node** head, char* word) {
    if(head != NULL) {
        struct node *temporary = *head;

        while (temporary->next != NULL) {
            if (strcmp(temporary->data, word) == 0)
                return 1;
            temporary = temporary->next;
        }
    }
    return 0;
}


void Print(struct node* head){
    struct node* temporary1 = head;
    while (temporary1 != NULL) {
        printf("%s \n", temporary1->data);
        temporary1 = temporary1->next;
    }
}

void Insert(struct node** head, char* word)
{
    struct node* newNode = (struct node*)malloc(sizeof(struct node*)+1);

    newNode->data = malloc(strlen(word)+1);
    newNode->data = strcpy(newNode->data,word);
    newNode->next = NULL;

    struct node* temporary3;

    if (*head == NULL || strcmp((*head)->data, newNode->data) >= 0) {
        newNode->next = *head;
        *head = newNode;
    }
    else {
        //Trovo il nodo precedente a dove voglio inserire il mio
        temporary3 = *head;
        while (temporary3->next != NULL && strcmp(temporary3->next->data, newNode->data) <0 ) {
            temporary3 = temporary3->next;
        }
        newNode->next = temporary3->next;
        temporary3->next = newNode;
    }
}

void Delete_Node(struct node** head, char* word) {
    if(head != NULL) {
        struct node *temporary4 = *head, *prev;

        if (temporary4 != NULL && strcmp(temporary4->data, word) == 0) {
            *head = temporary4->next;
            free(temporary4);
            return;
        }
        while (temporary4->next != NULL && strcmp(temporary4->data, word) != 0) {
            prev = temporary4;
            temporary4 = temporary4->next;
            prev->next = temporary4->next;
            free(temporary4);
        }
    }
    else
        return;
}

EDIT: Don't know if it could help, but if i try to run the program it stops and returns编辑:不知道它是否有帮助,但如果我尝试运行程序它会停止并返回

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

In your Search() function, you are comparing word to temporary->data , but you are checking for temporary->next in the while-loop.在您的Search() function 中,您将wordtemporary->data进行比较,但您正在while 循环中检查temporary->next This will skip the last node since its next pointer is set to NULL .这将跳过最后一个节点,因为它的next指针设置为NULL

Change it to将其更改为

bool Search(struct node** head, char* word)
{
    if (!head) return false;
    
    struct node *iter = *head;

    while (iter != NULL) {
        if (strcmp(iter->data, word) == 0)
            return true;
        iter = iter->next;
    }

    return false;
}
  • Give meaningful (and short) names to your variables.为您的变量提供有意义的(和简短的)名称。 For example, temporary doesn't describe what your variable does, iter is consise and clearly means "iterator".例如, temporary没有描述您的变量的作用, iter是简洁的并且清楚地表示“迭代器”。
  • Don't compare ->next s.不要比较->next Compare pointers directly.直接比较指针。
  • There's a bool type you can make use of.您可以使用一个bool类型。

The rest of yout code should follow the same guidelines.您代码的 rest 应遵循相同的准则。

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

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