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

C++链表遍历:如何实现分支内全部书籍的打印功能

没问题,我来帮你实现这个printall()函数!先基于你的描述梳理一下数据结构,然后一步步完成打印功能,还会附上测试代码确保能正常运行。

步骤1:定义基础数据结构

首先咱们先把你提到的分支(Branch)、书籍节点(BookNode)以及存储分支的容器定义出来,用结构体就很合适:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm> // 用于std::find_if

// 存储单本书籍信息的链表节点
struct BookNode {
    std::string author;
    std::string title;
    int copyCount;
    BookNode* next;

    // 构造函数简化节点初始化
    BookNode(std::string auth, std::string tit, int count) 
        : author(std::move(auth)), title(std::move(tit)), copyCount(count), next(nullptr) {}
};

// 分支结构体:包含分支名称和书籍链表的头指针
struct Branch {
    std::string name;
    BookNode* head;

    Branch(std::string branchName) : name(std::move(branchName)), head(nullptr) {}
};

// 全局存储所有分支的vector容器(也可以封装成类,这里先简化)
std::vector<Branch> branches;
步骤2:实现printall()函数

这个函数的核心逻辑是:先在vector里找到目标分支,然后遍历该分支的书籍链表,逐个打印每本书的信息。还要处理分支不存在、分支无书籍的边界情况:

void printall(const std::string& branchName) {
    // 查找目标分支
    auto branchIt = std::find_if(branches.begin(), branches.end(), 
        [&branchName](const Branch& b) { return b.name == branchName; });

    // 分支不存在的情况
    if (branchIt == branches.end()) {
        std::cout << "提示:分支「" << branchName << "」不存在!" << std::endl;
        return;
    }

    // 分支无书籍的情况
    BookNode* currentNode = branchIt->head;
    if (!currentNode) {
        std::cout << "分支「" << branchName << "」目前没有任何书籍。" << std::endl;
        return;
    }

    // 遍历链表打印所有书籍
    std::cout << "=== 分支「" << branchName << "」的书籍列表 ===" << std::endl;
    int bookNum = 1;
    while (currentNode != nullptr) {
        std::cout << "书籍 " << bookNum << ":" << std::endl;
        std::cout << "  作者: " << currentNode->author << std::endl;
        std::cout << "  书名: " << currentNode->title << std::endl;
        std::cout << "  副本数量: " << currentNode->copyCount << std::endl << std::endl;
        
        currentNode = currentNode->next;
        bookNum++;
    }
}
步骤3:测试代码(添加书籍并调用打印)

为了验证功能,咱们写一个辅助函数给分支添加书籍,然后在main里给Alex分支添加你提到的三本书,最后调用printall():

// 辅助函数:给指定分支添加书籍(如果分支不存在则自动创建)
void addBookToBranch(const std::string& branchName, const std::string& author, const std::string& title, int copyCount) {
    auto branchIt = std::find_if(branches.begin(), branches.end(), 
        [&branchName](const Branch& b) { return b.name == branchName; });

    // 分支不存在则创建
    if (branchIt == branches.end()) {
        branches.emplace_back(branchName);
        branchIt = branches.end() - 1;
    }

    // 创建新书籍节点,添加到链表尾部(保证添加顺序和打印顺序一致)
    BookNode* newBook = new BookNode(author, title, copyCount);
    if (!branchIt->head) {
        // 链表为空,直接作为头节点
        branchIt->head = newBook;
    } else {
        // 找到链表尾节点
        BookNode* tailNode = branchIt->head;
        while (tailNode->next != nullptr) {
            tailNode = tailNode->next;
        }
        tailNode->next = newBook;
    }
}

int main() {
    // 给Alex分支添加指定书籍
    addBookToBranch("Alex", "Stan Moon", "C++ 入门实战", 5);
    addBookToBranch("Alex", "Bill Sun", "Effective C++ 进阶指南", 3);
    addBookToBranch("Alex", "Chris Ground", "现代C++设计模式", 2);

    // 打印Alex分支的所有书籍
    printall("Alex");

    // 记得释放链表内存,避免内存泄漏(实际项目建议用智能指针替代裸指针)
    for (auto& branch : branches) {
        BookNode* current = branch.head;
        while (current != nullptr) {
            BookNode* temp = current;
            current = current->next;
            delete temp;
        }
        branch.head = nullptr;
    }

    return 0;
}
额外提示
  • 如果不需要严格按添加顺序打印,也可以把新节点加到链表头部,这样添加效率更高(不用遍历找尾节点);
  • 实际开发中建议用std::unique_ptr来管理链表节点,避免手动释放内存的麻烦,比如把BookNode*改成std::unique_ptr<BookNode>;
  • 可以把分支管理的逻辑封装成一个类(比如Library类),更符合面向对象的设计思路。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:57:34