You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言双向链表节点双向关联异常排查与代码修复求助

双向链表代码问题诊断与修复方案

现有代码存在的核心问题

1. Create(头插)函数逻辑错误

  • 赋值new_node->data的操作放在了malloc成功校验之前,若内存分配失败会触发空指针访问崩溃
  • 未判断当前链表是否为空(*H == NULL的场景),首次插入时直接执行(*H)->prev = new_node会访问空指针
  • 头插逻辑最后缺失将头指针更新为新节点的代码*H = new_node,导致插入的节点根本没有接入链表

2. Delete(删除头节点)函数逻辑错误

  • struct node *temp = H;类型不匹配,H是二级指针,需要取*H赋值给temp才能拿到当前头节点的地址
  • 删除原头节点后没有将新头节点的prev指针置空,破坏双向链表的合法性

3. 主函数switch分支逻辑错误

  • case4(反向显示)没有加break语句,执行完反向显示后会直接落入case5的逻辑,自动触发搜索操作,不符合菜单设计预期

4. DisplayB(反向遍历)函数逻辑错误

  • 当前实现直接从头节点往prev方向遍历,而头节点的prev始终为NULL,无法打印完整的反向链表

修复后的完整代码

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

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

void Create(struct node **H,char  val) {
    struct node *new_node;
    new_node = malloc(sizeof(struct node));
    // 先校验内存分配结果
    if(new_node == NULL){
        printf("RIP");
        return;
    }
    new_node->data = val;
    new_node->prev = NULL;
    new_node->next = *H;
    // 原链表非空时才需要修改原头节点的prev指针
    if(*H != NULL){
        (*H)->prev = new_node;
    }
    // 更新头指针为新插入的节点
    *H = new_node;
}

void Delete(struct node **H){
    if(*H != NULL){
        struct node *temp = *H;
        printf("Deleting last element with value: %c\n", temp->data);
        *H = temp->next;
        // 新头节点非空时将prev置空
        if(*H != NULL){
            (*H)->prev = NULL;
        }
        free(temp);
    }else{
        printf("Empty list.\n");
    }
}

void DisplayF(struct node *H){
    int i = 0;
    if(H == NULL){
        printf("Empty list\n");
        return;
    }
    while(H != NULL){
        i++;
        printf("Element #%d: %c\n",i, H->data);
        H = H->next;
    }
    return;
}

void DisplayB(struct node *H){
    int i = 0;
    if(H == NULL){
        printf("Empty list\n");
        return;
    }
    // 先遍历到链表尾节点
    struct node *tail = H;
    while(tail->next != NULL){
        tail = tail->next;
    }
    // 从尾节点反向遍历打印
    while(tail != NULL){
        i++;
        printf("Element #%d: %c\n",i, tail->data);
        tail = tail->prev;
    }
    return;
}

void Search(struct node *H,char val){
    int i = 0;
    while(H != NULL){
        i++;
        if(H->data == val){
            printf("Character '%c' was found in element %d\n",H->data,i);
            return;
        }
        H = H->next;
    }
    printf("The value you are looking for does not exist.\n");
}
int main(){
    struct node *head = NULL;
    int inp;
    char d;
    do{
        printf("1.Create\n2.Delete\n3.Display Forward\n4.Display Backward\n5.Search\n0.Exit\n");
        scanf("%d",&inp);
        switch (inp){
            case 1:
                printf("Enter the new node's character value: ");
                scanf(" %c",&d);
                Create(&head,d);
                break;
            case 2:
                Delete(&head);
                break;
            case 3:
                DisplayF(head);
                break;
            case 4:
                DisplayB(head);
                break; // 补充缺失的break
            case 5:
                printf("What character do you want to search for? ");
                scanf(" %c",&d);
                Search(head,d);
                break;
            case 0:
                exit(0);
            default:
                printf("Enter a valid option from the menu below:\n");
                break;
        }

    }while(inp != 0);
    return 0;
}

内容的提问来源于stack exchange,提问作者Unknown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 06:45:00