
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Create linked list from a given array in C++ Program
In this tutorial, we are going to learn how to create a linked list from the given array.
Let's see the steps to solve the problem.
Initialize the array with dummy data.
Write the struct node.
Iterate over the array.
Create a new node with the data.
Insert the new node into the linked list.
Print the linked list.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; struct Node { int data; Node* next; }; struct Node* newNode(int data) { Node* node = new Node; node->data = data; node->next = NULL; return node; } void insertNewNode(Node** root, int data) { Node* node = newNode(data); Node* ptr; if (*root == NULL) { *root = node; } else { ptr = *root; while (ptr->next != NULL) { ptr = ptr->next; } ptr->next = node; } } void printLinkedList(Node* root) { while (root != NULL) { cout << root->data << " -> "; root = root->next; } cout << "NULL" << endl; } Node* createLinkedList(int arr[], int n) { Node *root = NULL; for (int i = 0; i < n; i++) { insertNewNode(&root, arr[i]); } return root; } int main() { int arr[] = { 1, 2, 3, 4, 5 }, n = 5; Node* root = createLinkedList(arr, n); printLinkedList(root); return 0; }
Output
If you run the above code, then you will get the following result.
1 -> 2 -> 3 -> 4 -> 5 -> NULL
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Create new linked list from two given linked list with greater element at each node in C++ Program
- Program to find folded list from a given linked list in Python
- Python program to create a doubly linked list from a ternary tree
- Golang Program to reverse a given linked list.
- Create a linked list from two linked lists by choosing max element at each position in C++ Program
- Program to find linked list intersection from two linked list in Python
- Program to remove last occurrence of a given target from a linked list in Python
- Python Program to Create a Linked List & Display the Elements in the List
- Python program to create and display a Circular Linked List
- Python program to create and display a doubly linked list
- Python Program to Convert a given Singly Linked List to a Circular List
- C# program to create a List with elements from an array
- Golang program to access elements from a linked list
- Program to sort a given linked list into ascending order in python
- Golang Program to add the first node in a given linked list.

Advertisements