Singly Linked List: বেসিক ক্লাস স্ট্রাকচার ও ডিসপ্লে
লিংকড লিস্ট তৈরি শুরু করতে হয় একটি Node ক্লাস এবং একটি LinkedList কন্টেইনার ক্লাস দিয়ে।
Class Definitions
class LinkedList {
constructor() {
this.head = null; // লিস্টের প্রথম নোড
this.length = 0; // এলিমেন্ট কাউন্ট
}
}
// Node Class (Prototype-এ সংজ্ঞা)
LinkedList.prototype.Node = class {
constructor(data) {
this.data = data; // স্টোর করা মান
this.next = null; // পরবর্তী নোডের রেফারেন্স
}
};
Display Method (Traversal)
হেড থেকে শুরু করে next ফলো করে সব নোড ভিজিট করা।
display() {
let currNode = this.head;
let output = "";
while (currNode !== null) {
output += currNode.data + " -> ";
currNode = currNode.next;
}
console.log(output + "null");
}
টেস্টিং উদাহরণ
const list = new LinkedList();
// Manual Node Creation & Linking (for display demo)
const n1 = new LinkedList.Node(10);
const n2 = new LinkedList.Node(20);
const n3 = new LinkedList.Node(30);
list.head = n1;
n1.next = n2;
n2.next = n3;
list.length = 3;
list.display();
// Output: 10 -> 20 -> 30 -> null
ভিজ্যুয়াল: Traversal Process
START: currNode = HEAD (Node 10)
↓
LOOP 1: output += "10 -> " | currNode = currNode.next (Node 20)
↓
LOOP 2: output += "20 -> " | currNode = currNode.next (Node 30)
↓
LOOP 3: output += "30 -> " | currNode = currNode.next (null)
↓
END: currNode === null → STOP
Output: "10 -> 20 -> 30 -> null"
Node Structure Recap
| Field | Type | Description |
|---|---|---|
data |
Any | নোডে সংরক্ষিত মান (নাম্বার, স্ট্রিং, অবজেক্ট, ইত্যাদি)۔ |
next |
Node | null | পরবর্তী নোডের রেফারেন্স। শেষ নোডের জন্য null। |
পরবর্তী ডেভেলপমেন্ট স্টেপস
- insert(data, position) — এলিমেন্ট যোগ।
- remove(position) — এলিমেন্ট মুছে ফেলা।
- search(value) — মান খুঁজে বের করা।
- get(index) — ইনডেক্স দিয়ে এক্সেস।
- reverse() — লিস্ট উল্টে ফেলা।
নোট: এই বেস স্ট্রাকচার থেকে আপনি Stack, Queue, Graph Adjacency List ইত্যাদি বিল্ড করতে পারেন।