【问题标题】:LinkedList functions crash in cLinkedList函数在c中崩溃
【发布时间】:2014-12-08 15:54:54
【问题描述】:

我在 c 中实现了一个链表。当我测试它时,每当我达到显示功能或在最后插入时,程序就会崩溃。这些是函数:

    struct Node
    {
        char data;
        struct Node *next;
    };
    struct LinkedList
    {
        struct Node *head;
    };
       void insertAtBeginning(struct LinkedList *LL, char ele)
{
    struct Node *new = (struct Node*)malloc(sizeof(struct Node));
    new->data = ele;
    new->next = NULL;
    if(new->data == '\n')return;
    if(LL->head==NULL)LL->head = new;
    else
    {
        new->next=LL->head;
        LL->head=new;
    }
}
void insertAtTheEnd(struct LinkedList *LL, char ele)
{
    struct Node *new = (struct Node*) malloc(sizeof(struct Node));
    new->data = ele;
    new->next = NULL;
    if(LL->head==NULL){LL->head=new;return;}
    struct Node *current = LL->head;
    while(current->next != NULL) {current = current->next;}
    current->next = new;
}
void deleteNode(struct LinkedList* LL, char ele)
{
    struct Node *current = LL->head;
    struct Node *temp = LL->head;
    while(current->next->data!=ele && current!=NULL)current=current->next;
    temp=current->next; 
    current->next=current->next->next;
    free(temp);
}
void deleteFirstNode(struct LinkedList* LL)
{
    struct Node *temp;  
    if(LL->head != NULL)
    {
        temp = LL->head;
        LL->head = LL->head->next;
        free(temp);
    }
}
void displayLinkedList(struct LinkedList LL)
{
    struct Node *current = LL.head;
    printf("List: ");
    while(current != NULL)
    {
        printf("%c",current->data);
        current = current->next;
    }
        printf("\n");
}

这是主要的:

    main()
{
    struct LinkedList LL;
    char c = '0';
    //Inserting at beginning
    printf("Type a string. Press Enter to end: ");
    while(c != '\n')
    {
        scanf("%c",&c);
        insertAtBeginning(&LL, c);  
    }
    printf("List: ");
    displayLinkedList(LL);
    printf("\n");
    //Inserting at end
    c='0';
    printf("Type a string. Press Enter to end: ");
    while(c != '\n')
    {
        scanf("%c",&c);
        insertAtTheEnd(&LL, c);     
    }
    printf("List: ");
    displayLinkedList(LL);
    printf("\n");
    //Remove
    printf("Enter a char to remove: ");
    scanf("%c",&c);
    deleteNode(&LL, c);
    printf("\n");
    printf("List: ");
    displayLinkedList(LL);
    printf("\n");
    deleteFirstNode(&LL);
    printf("List: ");
    displayLinkedList(LL);
}

当然这是在插入必要的库之后完成的。

【问题讨论】:

  • 你的main 方法没有返回类型?
  • 在创建新节点后立即将其“下一个”设置为 NULL

标签: c linked-list nodes


【解决方案1】:

您必须在创建元素时将NULL 设置为下一个。如果你不这样做,你的 while 循环行为将是错误的

void insertAtTheEnd(struct LinkedList *LL, char ele)
{
    struct Node *new =  (struct Node*) malloc(sizeof(struct Node));
    new->next = NULL;
    new->data = ele;
    if(LL->head == NULL) LL->head = new;
    else
    {
    struct Node *current = LL->head;
    while(current->next != NULL) {current = current->next;}
    current->next = new;
    }
}

,其他功能也一样。

您还必须将NULL 设置为您的LinkedList.head。所以,在定义LinkedList时设置NULL

struct LinkedList LL;
LL.head = NULL;

【讨论】:

  • 好的。但它仍然在崩溃。同显示功能
  • 显示功能似乎没问题。我建议检查其他功能。函数之一可能仍然具有非初始化“下一个”属性
  • insertAtBeginning 循环后使用第一个显示时总是崩溃。问题出在这两个函数之一。
  • 问题可能是因为您在显示方法中使用了非指针 LinkedList。你能把它转换成指针,然后像其他函数一样发送你的 LinkedList 的地址。对于前两行显示函数应该是, void displayLinkedList(struct LinkedList* LL) { struct Node *current = LL->head; ...
  • @hzjw,我发现了问题。当您创建 LinkedList 本身时,您不会分配其标题。所以, if (LL->head== NULL) 永远不会起作用。你应该在定义它之后初始化 LinkedList 的头。所以,结构 LinkedList LL; LL.head = NULL;就足够了。我也建议在显示功能中使用指针
【解决方案2】:

我假设 if(current = LL->head) 错误
因为 temp = current->next 如果结果为 NULL 会崩溃 否则 if 语句后面的代码没有意义

【讨论】:

  • 如果current->nextNULL 它根本不会进入循环
  • 如果条件总是为真,那么 if 块后面的代码将永远无法到达,否则会因为 current 为 NULL 而崩溃
【解决方案3】:

在某些情况下,您会尝试在末尾插入。当你有node_new->next->prev = node 形式的东西时,你必须检查以确保node_new->next 不是NULL,因为NULL 没有任何类型,因此没有prev 字段。

你也可以考虑让你的所有函数都返回int,这样你就可以知道是否出了什么问题。

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    相关资源
    最近更新 更多