You need to enable JavaScript to run this app. C语言双向链表打印失败求助 疑getSongInfo或printSongInfo函数出错
优惠活动
大模型
产品
解决方案
定价
更多

C语言双向链表打印失败求助 疑getSongInfo或printSongInfo函数出错

问题排查与修正方案

现有代码的问题汇总

  • 结构体定义错误:typedef定义结构体时内部提前使用了未声明的SongNode类型,正确写法需要先声明结构体标签
  • main函数逻辑错误:getSongInfo调用写在break语句之后,永远不会被执行,导致链表始终为空
  • getSongInfo函数错误:
    • rating是结构体内置的int类型成员,不需要单独malloc分配内存,原代码强制把malloc返回的地址转成int赋值,会导致内存泄漏和数值异常
    • 判断artist内存分配是否成功时,错误判断了newBlock->rating == NULL,变量名写错
    • 传入的tail指针完全没有处理,没有实现双向链表的尾指针维护
  • printSongInfo函数错误:格式化输出时写了3个占位符(%s %s %d),但只传了ptr->title和ptr->artist两个参数,缺少ptr->rating,会导致输出异常甚至程序崩溃

tail指针的正确用法

双向链表的tail指针始终指向链表最后一个节点,使用场景包括:

  • 尾插新节点时不需要从头遍历,直接用tail定位插入位置,时间复杂度O(1)
  • 需要反向遍历链表时,可以直接从tail开始通过prev指针往前遍历
  • 头插法实现时,只有第一次插入新节点时需要给tail赋值,后续插入头结点tail保持不变;如果是尾插法,每次插入新节点都要更新tail指向新的尾节点

核心代码修正示例

首先修正结构体定义:

// 正确的结构体定义,先声明struct SongNode标签再typedef
typedef struct SongNode {
    char* title;
    char* artist;
    int rating;
    struct SongNode* next;
    struct SongNode* prev;
}SongNode;

修正main函数里的输入逻辑:

printf("Rating: ");
while (rating = getNum()) { 
    if (rating < 1 || rating>5) {
        printf("The number you have entered is invaild\n");
        rating = 0;
        printf("Rating: ");
        continue;
    }
    break;
}
// 把getSongInfo移到break外面,确保校验通过后执行
head=getSongInfo(&head, &tail, title, artist, rating); 
printf("\n");

修正getSongInfo函数,去掉rating的malloc,修正判断逻辑,增加tail维护:

SongNode* getSongInfo(SongNode** head, SongNode** tail, char title[], char artist[], int rating)
{
    SongNode *newBlock = NULL; 

    newBlock = (SongNode*)malloc(sizeof(SongNode)); 
    if (newBlock == NULL) {
        printf("No memory to be Allocated\n"); 
        return (*head); 
    }

    newBlock->title=(char*)malloc(strlen(title) + 1);
    if (newBlock->title == NULL) {
        printf("No memory can be allocated for title\n"); 
        free(newBlock);
        return (*head); 
    }

    newBlock->artist = (char*)malloc(strlen(artist) + 1); 
    // 修正判断条件,判断artist是否分配成功
    if (newBlock->artist == NULL) {
        printf("No memory can be alocated for artist\n"); 
        free(newBlock->title);
        free(newBlock);
        return (*head); 
    }

    // 删除rating的malloc逻辑,int不需要动态分配
    strcpy(newBlock->title, title); 
    strcpy(newBlock->artist, artist); 
    newBlock->rating = rating; 

    newBlock->next = (*head); 
    newBlock->prev = NULL; 

    if ((*head) != NULL) 
        (*head)->prev = newBlock; 
    else
        // 第一次插入节点,tail也指向新节点
        *tail = newBlock;

    (*head) = newBlock; 

    return (*head); 
}

修正printSongInfo函数,补全rating参数:

void printSongInfo(SongNode* head) {
    SongNode* ptr;
    ptr = head;

    printf("\n");
    printf("%-35s %-35s %-35s\n", "Title", "Artist","Rating");

    while (ptr != NULL) {
        // 补全第三个参数ptr->rating
        printf("%-35s %-35s %-35d\n", ptr->title, ptr->artist, ptr->rating);
        ptr = ptr->next;
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.15 10:00:05