C++ Linked List Exercises: Create and display linked list
C++ Linked List: Exercise-1 with Solution
Write a C++ program to create and display a Singly Linked List.
Visualization:

Test Data:
The list contains the data entered:
11 9 7 5 3 1
Sample Solution:
C++ Code:
#include <iostream>
using namespace std;
//Declare node
struct Node{
int num;
Node *next;
};
//Starting (Head) node
struct Node *head=NULL;
//Insert node at start
void insert_Node(int n){
struct Node *new_node=new Node;
new_node->num=n;
new_node->next=head;
head=new_node;
}
//Display all nodes
void display_all_nodes(){
cout<<"The list contains the data entered:\n";
struct Node *temp = head;
while(temp!=NULL){
cout<<temp->num<<" ";
temp=temp->next;
}
cout<<endl;
}
int main(){
insert_Node(1);
insert_Node(3);
insert_Node(5);
insert_Node(7);
insert_Node(9);
insert_Node(11);
display_all_nodes();
return 0;
}
Sample Output:
The list contains the data entered: 11 9 7 5 3 1
Flowchart:

CPP Code Editor:
Contribute your code and comments through Disqus.
Previous C++ Exercise: C++ Linked List Exercises Home
Next C++ Exercise: Reverse linked list.
What is the difficulty level of this exercise?
C++ Programming: Tips of the Day
Function for C++ struct
A struct is identical to a class except for the default access level (member-wise and inheritance-wise). (and the extra meaning class carries when used with a template)
Every functionality supported by a class is consequently supported by a struct. You'd use methods the same as you'd use them for a class.
struct foo { int bar; foo() : bar(3) {} //look, a constructor intgetBar() { return bar; } }; foo f; int y = f.getBar(); // y is 3
Ref: https://bit.ly/3ww1msg
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook