How to Create and Connect Nodes in a Linked List in Various Programming Languages
In this article, we’ll explore how to represent a basic linked list using several popular programming languages. We’ll see how to define a linked list node, create linked list nodes with necessary data, and link them together in a specific sequence.
Below is an example linked list which we’ll try to create programmatically:
Each node in a singly linked list needs enough memory to store two attributes:
Value/Data: This attribute holds the actual information or content that the node represents. It can be of any data type, such as an integer, string, or even a complex object, depending on the requirements of the linked list.
Pointer/Reference: This attribute stores the memory address (reference) of the next node in the sequence. It acts as a link, connecting the current node to the following one, allowing for traversal through the list. In the case of the last node (tail node), this pointer typically points to null, indicating the end of the list.
Taking into account the above requirements, below is how the definition of a singly linked list node looks like:
class Node:
def __init__(self, data):
# Initialize the node with given data
self.data = data
# Set the next pointer to None initially
self.next = None
// Node class to store data and link to the next node
class Node {
public:
string data;
Node* next;
// Initialize the node with given data
Node(string data) {
this->data = data;
// Set the next pointer to nullptr initially
this->next = nullptr;
}
};
class Node {
constructor(data) {
// Initialize the node with given data
this.data = data;
// Set the next pointer to null initially
this.next = null;
}
}
// Node class to store data and link to the next node
static class Node {
String data;
Node next;
// Initialize the node with given data
public Node(String data) {
this.data = data;
// Set the next pointer to null initially
this.next = null;
}
}
Creating Linked List Nodes
Let’s say we need to create a linked list to store elements A, B, C, D. We need to create a node for each element and initialize it’s data field with the corresponding element. The next pointers for each node can be set to null as we have not linked the created nodes yet.
Below is how to create and initialize the linked list nodes using various programming languages:
# Create a node for each element
node1 = Node("A")
node2 = Node("B")
node3 = Node("C")
node4 = Node("D")
// Function to create a new Node
Node* createNode(int data) {
// Allocate memory for the new Node
Node* newNode = (Node*)malloc(sizeof(Node));
// Initialize the node with given data
newNode->data = data;
// Set the next pointer to NULL initially
newNode->next = NULL;
return newNode;
}
int main() {
// Create a node for each element
struct Node *node1 = createNode('A');
struct Node *node2 = createNode('B');
struct Node *node3 = createNode('C');
struct Node *node4 = createNode('D');
// Other operations..
// Free the allocated memory
free(node1);
free(node2);
free(node3);
free(node4);
return 0;
}
int main() {
// Create a node for each element
Node* node1 = new Node("A");
Node* node2 = new Node("B");
Node* node3 = new Node("C");
Node* node4 = new Node("D");
// Other operations..
// Clean up memory
delete node1;
delete node2;
delete node3;
delete node4;
return 0;
}
// Create a node for each element
let node1 = new Node("A");
let node2 = new Node("B");
let node3 = new Node("C");
let node4 = new Node("D");
// Create a node for each element
Node node1 = new Node("A");
Node node2 = new Node("B");
Node node3 = new Node("C");
Node node4 = new Node("D");
Notice that in programming languages with manual memory management (like C and C++), we manually delete each node in the linked list after all required operations are complete. Missing this step will introduce memory leaks which causes programs to crash.
Linking all Created Nodes Together
The next step is to connect all the created linked list nodes together in a sequence. This is generally done by using the expression: node1.next=node2. After executing this expression, node1 will point to node2 as the next node in the sequence. Below is how this syntax looks in different programming languages:
# link/chain the nodes in sequence
node1.next = node2
node2.next = node3
node3.next = node4
# node1 is the head node in the linked list
head = node1
# node4 is the tail node in the linked list
tail = node4
# link/chain the nodes in sequence
node1->next = node2;
node2->next = node3;
node3->next = node4;
node4->next = NULL;
// node1 is the head node in the linked list
struct Node *head = node1;
// node4 is the tail node in the linked list
struct Node *tail = node4;
// link/chain the nodes in sequence
node1->next = node2;
node2->next = node3;
node3->next = node4;
// node1 is the head node in the linked list
Node* head = node1;
// node4 is the tail node in the linked list
Node* tail = node4;
// link/chain the nodes in sequence
node1.next = node2;
node2.next = node3;
node3.next = node4;
// node1 is the head node in the linked list
let head = node1;
// node4 is the tail node in the linked list
let tail = node4;
// link/chain the nodes in sequence
node1.next = node2;
node2.next = node3;
node3.next = node4;
// node1 is the head node in the linked list
Node head = node1;
// node4 is the tail node in the linked list
Node tail = node4;
Note that once a linked list is created, addresses/references to internal nodes like node2, node3 etc is not stored. Only the head and tail nodes are directly visible/accessible to the users of linked list. All other nodes can be accessed by traversing the linked list using head node.
Combining all the Steps
To summarize, below are all the steps involved in building a linked list:
Defining a linked list node
Creating a linked list node for each element which needs to be stored
Linking all the created nodes in a sequence
Below is the code in various programming languages which includes the above steps:
# Node class to store data and link to the next node
class Node:
def __init__(self, data):
# Initialize the node with given data
self.data = data
# Set the next pointer to None initially
self.next = None
# We need to create the nodes first before linking them
node1 = Node("A")
node2 = Node("B")
node3 = Node("C")
node4 = Node("D")
# Let us now link/chain the nodes in order
# to form a linked list
node1.next = node2
node2.next = node3
node3.next = node4
# node1 is the head node in the linked list
head = node1
# node4 is the tail node in the linked list
tail = node4
# let's print all the data using head node
current_node = head
while current_node != None:
print(current_node.data, end=" ")
current_node = current_node.next
#include <stdio.h>
#include <stdlib.h>
// Define the Node structure
typedef struct Node {
char data;
struct Node* next;
} Node;
// Function to create a new Node
Node* createNode(int data) {
// Allocate memory for the new Node
Node* newNode = (Node*)malloc(sizeof(Node));
// Initialize the node with given data
newNode->data = data;
// Set the next pointer to NULL initially
newNode->next = NULL;
return newNode;
}
int main() {
// Create and allocate memory for each node
struct Node *node1 = createNode('A');
struct Node *node2 = createNode('B');
struct Node *node3 = createNode('C');
struct Node *node4 = createNode('D');
// Link nodes in sequence to form a linked list
node1->next = node2;
node2->next = node3;
node3->next = node4;
node4->next = NULL;
// node1 is the head node in the linked list
// node4 is the tail node in the linked list
// Printing items in the linked list using the head node
struct Node *cnode = node1;
while (cnode != NULL) {
printf("%c ", cnode->data);
cnode = cnode->next;
}
// Let's free the allocated memory
free(node1);
free(node2);
free(node3);
free(node4);
return 0;
}
#include <iostream>
using namespace std;
// Node class to store data and link to the next node
class Node {
public:
string data;
Node* next;
// Initialize the node with given data
Node(string data) {
this->data = data;
// Set the next pointer to nullptr initially
this->next = nullptr;
}
};
int main() {
// We need to create the nodes first before linking them
Node* node1 = new Node("A");
Node* node2 = new Node("B");
Node* node3 = new Node("C");
Node* node4 = new Node("D");
// Let us now link/chain the nodes in order
// to form a linked list
node1->next = node2;
node2->next = node3;
node3->next = node4;
// node1 is the head node in the linked list
Node* head = node1;
// node4 is the tail node in the linked list
Node* tail = node4;
// let's print all the data using head node
Node* current_node = head;
while (current_node != nullptr) {
cout << current_node->data << " ";
current_node = current_node->next;
}
// Clean up memory
delete node1;
delete node2;
delete node3;
delete node4;
return 0;
}
// Node class to store data and link to the next node
class Node {
constructor(data) {
// Initialize the node with given data
this.data = data;
// Set the next pointer to null initially
this.next = null;
}
}
// We need to create the nodes first before linking them
let node1 = new Node("A");
let node2 = new Node("B");
let node3 = new Node("C");
let node4 = new Node("D");
// Let us now link/chain the nodes in order
// to form a linked list
node1.next = node2;
node2.next = node3;
node3.next = node4;
// node1 is the head node in the linked list
let head = node1;
// node4 is the tail node in the linked list
let tail = node4;
// let's print all the data using head node
let currentNode = head;
let output = "";
while (currentNode !== null) {
output += currentNode.data + " ";
currentNode = currentNode.next;
}
console.log(output.trim());
public class Main {
// Node class to store data and link to the next node
static class Node {
String data;
Node next;
// Initialize the node with given data
public Node(String data) {
this.data = data;
// Set the next pointer to null initially
this.next = null;
}
}
public static void main(String[] args) {
// We need to create the nodes first before linking them
Node node1 = new Node("A");
Node node2 = new Node("B");
Node node3 = new Node("C");
Node node4 = new Node("D");
// Let us now link/chain the nodes in order
// to form a linked list
node1.next = node2;
node2.next = node3;
node3.next = node4;
// node1 is the head node in the linked list
Node head = node1;
// node4 is the tail node in the linked list
Node tail = node4;
// let's print all the data using head node
Node currentNode = head;
while (currentNode != null) {
System.out.print(currentNode.data + " ");
currentNode = currentNode.next;
}
}
}
After linking all the nodes, we also added a block of code to print values present in all the nodes in the linked list. You can go through this article on linked list traversal to learn more about how it works.