जावास्क्रिप्ट में एल्गोरिदम और डेटा संरचनाएँ: व्यवहार में
आधुनिक वेब डेवलपमेंट में, एल्गोरिदम और डेटा संरचनाओं का ज्ञान केवल एक शैक्षणिक कौशल नहीं है, बल्कि तेज़ और स्केलेबल एप्लिकेशन बनाने का एक उपकरण है। JavaScript, विशेष रूप से Node.js वातावरण में, सरल और जटिल दोनों प्रकार की एल्गोरिदमिक संरचनाओं को लागू करने की अनुमति देता है। इस लेख में, हम प्रमुख डेटा संरचनाओं और एल्गोरिदम को व्यवहार में समझेंगे, कोड उदाहरणों के साथ जिन्हें आप तुरंत अपने प्रोजेक्ट में उपयोग कर सकते हैं।
1. मूलभूत डेटा संरचनाएँ: स्टैक, क्यू और लिंक्ड लिस्ट
आइए मूल बातों से शुरू करें। स्टैक और क्यू रैखिक संरचनाएँ हैं, जिनका उपयोग अक्सर पार्सिंग, टास्क क्यू और एक्शन हिस्ट्री प्रबंधन में किया जाता है।
स्टैक (LIFO)
स्टैक "अंतिम आया, पहला गया" सिद्धांत पर काम करता है। इसे ऐरे के आधार पर लागू करते हैं:
class Stack { constructor() { this.items = []; }
push(element) { this.items.push(element); }
pop() { return this.items.pop(); }
peek() { return this.items[this.items.length - 1]; }
isEmpty() { return this.items.length === 0; }}
// उपयोग का उदाहरणconst stack = new Stack();stack.push('Page 1');stack.push('Page 2');console.log(stack.pop()); // 'Page 2'क्यू (FIFO)
क्यू कार्यों को आने के क्रम में संसाधित करने के लिए उपयोगी है। क्लास का उपयोग करके कार्यान्वयन:
class Queue { constructor() { this.items = {}; this.front = 0; this.rear = 0; }
enqueue(element) { this.items[this.rear] = element; this.rear++; }
dequeue() { const item = this.items[this.front]; delete this.items[this.front]; this.front++; return item; }
peek() { return this.items[this.front]; }
size() { return this.rear - this.front; }}
// उदाहरणconst queue = new Queue();queue.enqueue('Task 1');queue.enqueue('Task 2');console.log(queue.dequeue()); // 'Task 1'लिंक्ड लिस्ट
लिंक्ड लिस्ट सूची के बीच में तत्वों को सम्मिलित और हटाने के लिए कुशल है। एकल लिंक्ड लिस्ट लागू करते हैं:
class Node { constructor(value) { this.value = value; this.next = null; }}
class LinkedList { constructor() { this.head = null; this.tail = null; this.length = 0; }
append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.tail.next = newNode; this.tail = newNode; } this.length++; }
prepend(value) { const newNode = new Node(value); newNode.next = this.head; this.head = newNode; if (!this.tail) { this.tail = newNode; } this.length++; }
print() { const arr = []; let current = this.head; while (current) { arr.push(current.value); current = current.next; } return arr; }}
// उदाहरणconst list = new LinkedList();list.append(10);list.append(20);list.prepend(5);console.log(list.print()); // [5, 10, 20]2. सर्च एल्गोरिदम: लीनियर और बाइनरी
सर्च सबसे सामान्य ऑपरेशनों में से एक है। दो दृष्टिकोणों पर विचार करें: सरल लीनियर सर्च और कुशल बाइनरी सर्च।
लीनियर सर्च
असॉर्टेड डेटा के लिए उपयुक्त। जटिलता O(n):
function linearSearch(arr, target) { for (let i = 0; i < arr.length; i++) { if (arr[i] === target) { return i; // इंडेक्स लौटाता है } } return -1;}
console.log(linearSearch([3, 7, 1, 9, 4], 9)); // 3बाइनरी सर्च
सॉर्टेड ऐरे की आवश्यकता होती है। जटिलता O(log n)। Node.js पर कार्यान्वयन:
function binarySearch(arr, target) { let left = 0; let right = arr.length - 1;
while (left <= right) { const mid = M