Arrays तब शानदार हैं जब data का size पहले से पता हो। लेकिन जब data unpredictably आए - sensor readings की stream, AI के लिए tasks की queue, या हर message के साथ बढ़ती conversation history? ऐसे में structures चाहिए जो smoothly बड़े-छोटे हो सकें।
यहाँ आते हैं linked lists, stacks, और queues।
Linked list items को nodes की chain में store करती है। हर node दो चीज़ें रखता है: data और अगले node का pointer।
[data: "A" | next: →] → [data: "B" | next: →] → [data: "C" | next: null]
Arrays के विपरीत, linked list के nodes memory में अलग-अलग जगह हो सकते हैं - pointer बताता है अगला कहाँ मिलेगा।
| Operation | Array | Linked List | |-----------|-------|-------------| | Index से access | O(1) ⚡ | O(n) 🐢 | | शुरू में insert | O(n) 🐢 | O(1) ⚡ | | बीच में insert | O(n) 🐢 | O(1)* ⚡ | | बीच से delete | O(n) 🐢 | O(1)* ⚡ | | Memory usage | Compact | Extra (pointers) |
*Position मिलने के बाद - ढूँढना अभी भी O(n) है।
Browser का tab bar tabs खोलने, बंद करने, और rearrange करने देता है। Open tabs manage करने के लिए array बेहतर या linked list? बीच का tab बंद करने पर क्या होगा?
Stack plates की stack जैसा काम करता है: ऊपर से रखो, ऊपर से उठाओ। आखिरी रखा item पहले निकलता है।
Push "A" → [A]
Push "B" → [A, B]
Push "C" → [A, B, C]
Pop → [A, B] (removed "C")
Pop → [A] (removed "B")
दो operations define करते हैं stack को:
Sign in to join the discussion
दोनों O(1) - stack कितना भी बड़ा हो, instant।
"Stack overflow" error का मतलब literally है call stack की space खत्म हो गई - usually infinite recursion की वजह से। मशहूर Q&A site Stack Overflow इसी error से named है!
Drawing app के लिए 'undo' feature implement कर रहे हो। Actions की history के लिए कौन सा data structure सबसे अच्छा?
Queue दुकान की line जैसी है: पहले आया, पहले served। Items पीछे से आते हैं और आगे से निकलते हैं।
Enqueue "A" → [A]
Enqueue "B" → [A, B]
Enqueue "C" → [A, B, C]
Dequeue → [B, C] (removed "A")
Dequeue → [C] (removed "B")
AI API 10,000 requests/second receive करती है। Arrival order में process करने के लिए कौन सा structure?
Priority queue भी होती है, जहाँ items arrival order की जगह priority से dequeue होते हैं। AI systems urgent tasks पहले process करने में priority queues इस्तेमाल करते हैं।
Language models text sequences process करते हैं। PyTorch और TensorFlow जैसे frameworks linked-list जैसी structures से computation graphs बनाते हैं - operations की chains जहाँ हर step अगले को point करता है।
Neural network training में memory लगातार allocate और free होती है। Memory allocators अक्सर free blocks की linked lists इस्तेमाल करते हैं।
Chatbot को conversation की आखिरी 10 messages याद रखनी हैं, पुरानी discard करनी हैं। Stack इस्तेमाल करोगे, queue, या कुछ और? 11वीं message आए तो?
Linked lists के बारे में कौन सा statement FALSE है?