Print Linked List using C++ Programming Language

Author: Al-mamun Sarkar Date: 2020-04-16 14:47:17

Print Linked List using C++ Programming Language. The following c++ codes show how to print Linked List using a loop.

 

Code:

#include
using namespace std;

struct Node
{
    int data;
    Node* next;
};

Node *head;

// Ia an item to Link list
void Insert(int x)
{
    Node *temp = new Node();
    temp->data = x;
    temp->next = head;
    head = temp;

}

// Print link list
void Print()
{
    Node *temp = head;
    cout << "List is : ";
    while (temp != NULL) {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << endl;
}

int main()
{
    head = NULL;
    cout << "How many numbers : ";
    int n, x;

    cin >> n;

    for (int i=0; i> x;
        Insert(x);
        Print();
    }

    return 0;
}