Singly, Doubly and Circular Linked List Explained in Java (Complete Beginner Guide & With Implementation)
Types of Linked List (Complete Student Friendly Guide)
Why So Many Types?
Linked List ek flexible data structure hai.
Different problems ke liye different versions use kiye jate hain.
Jaise gaadi ke alag models hote hain —
waise hi Linked List ke alag types hote hain.
Main types:
Singly Linked List
Doubly Linked List
Circular Linked List
Singly Linked List
Definition
A Singly Linked List is the simplest form of linked list where each node contains two parts: data and a reference to the next node. The list starts with a head node, which acts as the entry point, and ends with a node that points to null, indicating the end of the structure. Each node only knows about the next node, not the previous one. This makes traversal possible only in one direction. Singly Linked Lists are memory efficient compared to doubly linked lists and are commonly used when backward traversal is not required. In a singly linked list, traversal is possible only in one direction — forward. This structure is memory efficient because each node stores only one reference. However, operations such as backward traversal or deletion of a previous node require additional traversal from the head, making some operations less efficient compared to doubly linked lists.
Real Life Example
Socho treasure hunt game.
Har clue me sirf next location ka address likha hai.
Tum peeche nahi ja sakte.
Sirf aage badh sakte ho.
Ye Singly Linked List hai.
Java Example
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
Doubly Linked List
Definition
A Doubly Linked List is a linear data structure where each node contains three parts: data, a reference to the next node, and a reference to the previous node. Unlike a singly linked list, it allows traversal in both forward and backward directions. The first node’s previous reference is null, and the last node’s next reference is null. Doubly linked lists provide more flexibility in navigation and deletion but consume more memory due to the extra pointer stored in each node.
A Doubly Linked List is an advanced version of a linked list in which each node contains three parts: data, a reference to the next node, and a reference to the previous node. This bidirectional linking allows traversal in both forward and backward directions. The first node’s previous reference is null, and the last node’s next reference is null. Due to the presence of two references, doubly linked lists consume more memory than singly linked lists. However, they provide more flexibility and efficient deletion of nodes because access to the previous node is directly available without needing full traversal from the head.
Real Life Example
Socho train me compartments connected hain.
Har compartment ko pata hai:
Aage ka compartment
Peeche ka compartment
Tum aage bhi ja sakte ho, peeche bhi.
Ye Doubly Linked List hai.
Java Example
class DNode {
int data;
DNode next;
DNode prev;
DNode(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
Circular Linked List
Definition
A Circular Linked List is a variation of linked list where the last node does not point to null but instead points back to the first node, forming a circular structure. This allows continuous traversal of the list without encountering a termination point. Circular linked lists can be singly circular (one reference) or doubly circular (two references). They are commonly used in applications that require repeated cycling through elements, such as round-robin scheduling in operating systems. Since there is no null termination, special care must be taken during traversal to avoid infinite loops.Circular linked lists are useful in applications where continuous looping is required, such as round-robin scheduling. They can be singly circular or doubly circular depending on the number of references stored in each node.
Real Life Example
Socho ek circular race track hai.
Start point par pahunchne ke baad race khatam nahi hoti —
tum fir se same track par aa jate ho.
Ye Circular Linked List hai.
Java Example (Circular Singly)
class CNode {
int data;
CNode next;
CNode(int data) {
this.data = data;
this.next = null;
}
}
Last node ka next head ko point karega.
Comparison Table
| Feature | Singly | Doubly | Circular |
|---|---|---|---|
| Direction | One way | Two way | Circular |
| Memory | Less | More | Moderate |
| Backward Traversal | No | Yes | Depends |
| End Node | Points to null | Points to null | Points to head |
When to Use Which?
Singly Linked List
Jab memory save karni ho.Doubly Linked List
Jab backward traversal zaruri ho.Circular Linked List
Round robin scheduling ke liye.Singly Linked List
Jab memory important ho aur backward movement required na ho.-
Doubly Linked List
Jab frequent forward-backward navigation required ho. -
Circular Linked List
Scheduling systems me.
Quick Basic Question
Doubly linked list me ek node me kitne references hote hain?
Thinking Question
Agar circular linked list me traversal karte waqt null check karoge, to kya hoga? Kyu?
Comments
Post a Comment