迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中打印链接列表

作者:迹忆客 最近更新:2023/03/23 浏览次数:

本文将介绍几种在 C++ 中打印链接列表元素的方法。

使用自定义函数打印链接列表中的元素

在下面的例子中,我们手动构建一个链接列表数据结构,用任意值初始化它,然后将元素打印到控制台。实现的结构是一个单一的链接列表,有三个数据成员,分别称为 citycountrykey

函数 addNewNode 用于在链接列表中构造一个新元素。它以 Node* 参数作为构建新节点的地址,以及需要为其数据成员分配的 3 个对应值。

由于我们是手动构建数据结构,我们需要利用动态内存分配。因此,在程序退出之前,需要另一个函数 freeNodes 来释放链表。

一旦完成了链接列表的初始化,我们就可以在循环中调用 printNodeData 函数来打印从 vector 对中推送到列表中的相同数量的元素。该函数只取类型为 Node*的参数,并调用 cout 将每个数据成员输出到控制台。这个函数的缺点是,用户每次需要打印元素时,都需要担心是否正确迭代到链接列表中。

#include<iostream>
#include<string>
#include<vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::pair;

struct Node {
    struct Node *next{};
    string city;
    string country;
    int key{};
};

struct Node *addNewNode(struct Node *node, int key, string &city, string &country) {
    node->next = new Node;
    node->next->key = key;
    node->next->city = city;
    node->next->country = country;
    return node;
}

void freeNodes(struct Node *node) {
    struct Node *tmp = nullptr;
    while (node) {
        tmp = node;
        node = node->next;
        delete tmp;
    }
}

void printNodeData(struct Node *node) {
    cout << "key: " << node->key << endl
        << "city: " << node->city << endl
        << "county: " << node->country << endl << endl;
}

int main() {
    struct Node *tmp, *root;
    struct Node *end = nullptr;

    vector<pair<string, string>> list = {{"Tokyo",  "Japan"},
                              {"New York",  "United States"},
                              {"Mexico City",  "Mexico"},
                              {"Tangshan",  "China"},
                              {"Tainan",  "Taiwan"}};

    root = new Node;
    tmp = root;
    for (int i = 0; i < list.size(); ++i) {
        tmp = addNewNode(tmp, i+1, list[i].first, list[i].second);
        tmp = tmp->next;
    }

    tmp = root->next;
    for (const auto &item : list) {
        printNodeData(tmp);
        tmp = tmp->next;
    }

    freeNodes(root->next);
    delete root;

    return EXIT_SUCCESS;
}

输出:

key: 1
city: Tokyo
county: Japan
...

使用自定义的函数打印链接列表中的所有元素

print 函数更好的实现是只调用一次的函数。printNodes 函数被定义为 void 类型,它不向调用者返回任何东西。它正好需要一个类型为 Node*的参数,类似于前面的函数,它自己在链接列表中进行迭代。注意,调用 freeNodes 函数还不足以清理数据结构使用的所有动态内存。从 main 函数中分配的 root 指针也需要释放,否则,内存泄漏将不可避免。

#include<iostream>
#include<string>
#include<vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::pair;

struct Node {
    struct Node *next{};
    string city;
    string country;
    int key{};
};

struct Node *addNewNode(struct Node *node, int key, string &city, string &country) {
    node->next = new Node;
    node->next->key = key;
    node->next->city = city;
    node->next->country = country;
    return node;
}

void freeNodes(struct Node *node) {
    struct Node *tmp = nullptr;
    while (node) {
        tmp = node;
        node = node->next;
        delete tmp;
    }
}

void printNodes(struct Node *node) {
    while (node){
        cout << "key: " << node->key << endl
            << "city: " << node->city << endl
            << "county: " << node->country << endl << endl;
        node = node->next;
    }
}

int main() {
    struct Node *tmp, *root;
    struct Node *end = nullptr;

    vector<pair<string, string>> list = {{"Tokyo",  "Japan"},
                              {"New York",  "United States"},
                              {"Mexico City",  "Mexico"},
                              {"Tangshan",  "China"},
                              {"Tainan",  "Taiwan"}};

    root = new Node;
    tmp = root;
    for (int i = 0; i < list.size(); ++i) {
        tmp = addNewNode(tmp, i+1, list[i].first, list[i].second);
        tmp = tmp->next;
    }

    printNodes(root->next);

    freeNodes(root->next);
    delete root;

    return EXIT_SUCCESS;
}

输出:

key: 1
city: Tokyo
county: Japan
...

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便