Theory
यह क्यों ज़रूरी है
जब भी आप Ctrl+Z दबाकर undo करते हैं, browser का Back button click करते हैं, या एक function दूसरे function को call करता है, तब पीछे चुपचाप एक stack ही काम कर रहा होता है। यह सबसे simple data structures में से एक है, और साथ ही सबसे ज़्यादा use होने वालों में से भी। इसे एक बार अच्छे से समझ लिया तो आगे के बहुत सारे topics (recursion, expression evaluation, parsing) एकदम से clear हो जाएँगे।
Theory
plates के ढेर की तरह सोचिए
आप साफ plate को ढेर के top पर रखते हैं, और जब ज़रूरत होती है तो भी top से ही उठाते हैं। बीच में से plate कभी नहीं खींचते। जो plate सबसे आखिर में रखी थी, वही सबसे पहले उठती है। बस यही एक rule पूरी कहानी है: LIFO, Last In, First Out.
Theory
मुख्य idea
Stack एक linear list है जिसमें बस एक restriction है: सारा काम एक ही end पर होता है, जिसे top कहते हैं।
- एक pointer (जिसे अक्सर
topकहते हैं) याद रखता है कि stack का top कहाँ है। - आप सिर्फ top पर ही insert या delete कर सकते हैं, बीच में या नीचे कभी नहीं।
- इसी वजह से जो element सबसे आखिर में add हुआ वही सबसे पहले remove होता है (LIFO)।
Theory
operations
- Push, top पर एक नया item रखना।
topएक ऊपर चला जाता है। - Pop, top का item हटाना और उसे return करना।
topएक नीचे आ जाता है। - Peek / Peep / Display, top का item हटाए बिना सिर्फ देख लेना।
- isEmpty, stack खाली है क्या? (
topकिसी पर point नहीं कर रहा) - isFull, fixed-size array वाले stack में, जगह बची है या नहीं?
Practical
Visualize it
This step has an interactive visualizer in Gri-Learn on the web.
Think first
मन में trace कीजिए
एक खाली stack से शुरू कीजिए। आप push 5, push 9, pop, फिर push 2 करते हैं। 5 second लीजिए: क्या pop हुआ, और अभी top पर क्या है?
Show the answer
pop ने 9 को हटाया, क्योंकि वही सबसे हाल में push हुआ था (LIFO)। इसके बाद 2 push करने पर stack नीचे से ऊपर की तरफ 5, 2 है, तो top पर 2 है। अगर आपने कहा कि pop ने 5 हटाया, तो वो queue का behaviour है (FIFO), stack का नहीं।
Watch out
exam के लिए 2 errors याद रखिए
Overflow तब होता है जब आप ऐसे stack पर push करते हैं जो पहले से ही full है (array वाले stack का size fixed होता है)। Underflow तब होता है जब आप खाली stack से pop (या peek) करते हैं। अच्छा code push से पहले isFull() और pop से पहले isEmpty() check करता है।
Watch
Data structures: Introduction to Stack
Watch on YouTube · mycodeschool
Theory
इसे implement कैसे करते हैं
सबसे simple implementation में एक array होता है और एक integer top index, जो -1 से शुरू होता है (यानी empty)।
- push(x): अगर full नहीं है, तो
top = top + 1करो फिरarr[top] = x। - pop(): अगर empty नहीं है, तो
arr[top]पढ़ो फिरtop = top - 1करो। - peek():
arr[top]return करो (topको मत बदलो)।
नीचे एक चलता हुआ version है, उसे change कीजिए, run कीजिए, और output देखिए।
Practical
एक छोटा array-based stack, run करके tweak कीजिए
// A simple fixed-size stack. Press Run, then try changing the values.
class Stack {
constructor(capacity) {
this.items = [];
this.capacity = capacity;
}
isFull() { return this.items.length === this.capacity; }
isEmpty() { return this.items.length === 0; }
push(x) {
if (this.isFull()) { console.log("Overflow! cannot push", x); return; }
this.items.push(x);
console.log("push", x, "-> top is now", this.peek());
}
pop() {
if (this.isEmpty()) { console.log("Underflow! stack is empty"); return; }
const x = this.items.pop();
console.log("pop ->", x);
return x;
}
peek() { return this.items[this.items.length - 1]; }
}
const s = new Stack(3);
s.push(10);
s.push(20);
s.push(30);
s.push(40); // full -> overflow
s.pop();
s.pop();
console.log("top after two pops:", s.peek());
This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
capacity 3 वाला एक stack खाली है। आप चलाते हैं: push A, push B, push C, push D, pop. अब peek() क्या return करेगा?
- B
- C
- D
- A
Show the answer
B
push D तब आता है जब stack पहले से ही full है, इसलिए वो reject हो जाता है (Overflow) और कुछ नहीं बदलता। फिर pop top यानी C को हटा देता है। बचता है A, B, तो peek() B return करेगा। अगर आपने C कहा तो आप overflow चूक गए; D तो अंदर आया ही नहीं।
Formula
stack कहाँ-कहाँ दिखता है
Function calls ("call stack"), undo/redo, browser history (Back), balanced parentheses ()[]{} check करना, और expressions को convert/evaluate करना (infix → postfix / prefix)। ये सब classic exam applications हैं, कम से कम 3 याद रखिए।
Summary
Key takeaways
- Stack एक LIFO list है, सारा काम top पर होता है।
- Push top पर add करता है; Pop top से remove करता है; Peek/Peep top को हटाए बिना पढ़ता है।
- Overflow = full stack पर push; Underflow = empty stack पर pop।
- Array implementation: एक
topindex जो -1 से शुरू होता है, push पर ऊपर और pop पर नीचे जाता है। - इसमें use होता है: call stack, undo/redo, browser back, balanced parentheses, infix→postfix/prefix।