Implementing a Singly Linked List in
C / C++ / Java
When working with dynamic data, arrays can be limiting because of their fixed size. That’s where Linked Lists come into play.
In this, you’ll learn what a Singly Linked List is, how it works, and how to implement it in C, C++, and Java.
What is a Linked List?
A Linked List is a linear data structure where elements are stored in nodes, and each node contains:
-
Data – The value or information.
-
Pointer (next) – The reference to the next node in the list.
Unlike arrays, linked lists don’t store elements in contiguous memory locations.
Structure of a Node
Each node looks like this:
[ Data | Next ]
The head points to the first node, and the last node points to NULL.
Example:
Head → [10|*] → [20|*] → [30|NULL]
Common Operations on Linked List
-
Insertion – Add a node at beginning, end, or specific position
-
Deletion – Remove a node
-
Traversal – Visit all nodes to display data
-
Search – Find a node with a given value
Implementation in C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Function to print linked list
void printList(struct Node* n) {
while (n != NULL) {
printf("%d -> ", n->data);
n = n->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
// Allocate nodes
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
head->data = 10;
head->next = second;
second->data = 20;
second->next = third;
third->data = 30;
third->next = NULL;
printList(head);
return 0;
}
Output:
10 -> 20 -> 30 -> NULL
Implementation in C++
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = NULL;
}
};
void printList(Node* n) {
while (n != NULL) {
cout << n->data << " -> ";
n = n->next;
}
cout << "NULL" << endl;
}
int main() {
Node* head = new Node(10);
Node* second = new Node(20);
Node* third = new Node(30);
head->next = second;
second->next = third;
printList(head);
return 0;
}
Output:
10 -> 20 -> 30 -> NULL
Implementation in Java
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
public class LinkedListDemo {
Node head;
public void printList() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.println("NULL");
}
public static void main(String[] args) {
LinkedListDemo list = new LinkedListDemo();
list.head = new Node(10);
Node second = new Node(20);
Node third = new Node(30);
list.head.next = second;
second.next = third;
list.printList();
}
}
Output:
10 -> 20 -> 30 -> NULL
Summary Table
| Operation | Description |
|---|---|
| Insertion | Add new node at desired position |
| Deletion | Remove node by key or position |
| Traversal | Visit all nodes |
| Search | Locate a node by value |
| Memory Type | Dynamic (uses heap) |
| Flexibility | High (size grows as needed) |
Advantages of Linked Lists
Dynamic memory allocation
Easy insertion and deletion
No need for resizing
Disadvantages
* Slower access (no direct indexing)
* Extra memory for pointers
Real-Life Analogy
Think of linked lists as a chain of paperclips — each clip (node) is connected to the next one, and you can easily add or remove a clip anywhere without reorganizing the entire chain.
Conclusion
Linked Lists are an essential foundation for understanding more complex structures like Stacks, Queues, and Trees.
Once you master these basics, you’ll find dynamic data handling in programming much easier!
.png)
Comments
Post a Comment