Print Linked List in C++

  1. Use Custom Defined Function to Print Element in Linked List
  2. Use Custom Defined Function to Print All Elements in Linked List

This article will explain several methods of how to print elements of a linked list in C++.

Use Custom Defined Function to Print Element in Linked List

In the following examples, we construct a linked list data structure manually, initialize it with arbitrary values, and then print the elements to the console. The implemented structure is a singly linked list with three data members called city, country, and key.

The function addNewNode is used to construct a new element in a linked list. It takes the Node* argument as the address where to construct a new node and 3 corresponding values that need to be assigned to its data members.

Since we are building the data structure manually, we need to utilize dynamic memory allocation. Thus another function, freeNodes, is needed to deallocate the linked list before the program exits.

Once the initialization of the linked list is done, we can call the printNodeData function in the loop to print the same number of elements that were pushed to the list from the vector of pairs. The function takes the only argument of type Node* and calls cout to output each data member to the console. The downside of this function is that the user needs to worry about correct iteration into the linked list every time he needs to print the elements.

#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;
}

Output:

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

Use Custom Defined Function to Print All Elements in Linked List

Better implementation of the print function would be the one that’s called only once. The printNodes function is defined as the type void that doesn’t return anything to the caller. It takes exactly one argument of type Node* similar to the previous function, and it does the iteration through the linked list all by itself. Notice that calling the freeNodes function is not enough to clean up all the dynamic memory used by the data structure. root pointer allocated from the main function needs to be freed as well; otherwise, a memory leak will be unavoidable.

#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;
}

Output:

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