PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Stack Operations: Push, Pop, Peek, IsEmpty, IsFull (स्टैक ऑपरेशन्स: पुश (ओवरफ्लो), पॉप (अंडरफ्लो) और पीक) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Core Stack operations—Push, Pop, Peek, isEmpty, and isFull—all execute in O(1) time with strict boundary guards to prevent memory corruption. Checking isFull avoids Stack Overflow on insertion, while checking isEmpty guards Pop and Peek against Stack Underflow.
अध्याय सार: स्टैक के मुख्य ऑपरेशन्स—Push, Pop, Peek, isEmpty और isFull—सभी O(1) समय में निष्पादित होते हैं और मेमोरी त्रुटियों से बचने के लिए सीमा जांच पर निर्भर करते हैं। isFull स्टैक ओवरफ्लो को रोकता है, जबकि isEmpty पॉप और पीक के समय स्टैक अंडरफ्लो से सुरक्षा करता है।
Stack Operations: Push, Pop, Peek, IsEmpty, IsFull: Detailed Notes & Architecture (स्टैक ऑपरेशन्स: पुश (ओवरफ्लो), पॉप (अंडरफ्लो) और पीक)
1. Push Operation & Stack Overflow Protection (Push ऑपरेशन और स्टैक ओवरफ्लो सुरक्षा)
[EN] Push(item) adds a new element onto the top of the stack in O(1) time. Before writing into the underlying array, it MUST evaluate isFull() (top == MAX - 1): if true, the stack cannot accept more data and raises Stack Overflow to prevent out-of-bounds buffer corruption. When space is available, Push increments top first (top = top + 1) and assigns stack[top] = item.
[HI] Push(item) स्टैक के शीर्ष पर नया तत्व O(1) समय में डालता है। डेटा लिखने से पहले यह जांचना अनिवार्य है कि स्टैक पूरा भरा तो नहीं है (isFull: top == MAX - 1); यदि भरा हो तो Stack Overflow की स्थिति आती है। जगह होने पर पहले top को 1 बढ़ाया जाता है और फिर stack[top] = item लिखा जाता है।
2. Pop Operation & Stack Underflow Protection (Pop ऑपरेशन और स्टैक अंडरफ्लो सुरक्षा)
[EN] Pop() removes and returns the current topmost element in O(1) time. It MUST first verify !isEmpty() (top != -1): attempting to pop from an empty stack triggers Stack Underflow. When non-empty, Pop reads val = stack[top] and decrements top (top--); notice that array slots above top are NOT physically zeroed out—logical deletion by moving top down is sufficient and fast.
[HI] Pop() स्टैक के सबसे ऊपरी तत्व को हटाकर वापस लौटाता है। इससे पहले !isEmpty() (top != -1) जांचना जरूरी है, अन्यथा खाली स्टैक से डेटा निकालने पर Stack Underflow एरर आती है। तत्व मौजूद होने पर stack[top] पढ़कर top-- कर दिया जाता है; पुराने स्लॉट की मेमोरी मिटाने की आवश्यकता नहीं होती।
3. Peek (Top) Operation: Non-Destructive Inspection (Peek (Top) ऑपरेशन: बिना हटाए शीर्ष मान देखना)
[EN] Peek()—named top() in C++ STL—returns the value of the topmost element WITHOUT decrementing top or altering stack size. Like Pop(), Peek must guard against isEmpty() because reading stack[-1] causes undefined behavior or garbage output. Note a subtle language trap: in C++ std::stack, pop() returns void and only removes the element, so programmers must call top() before pop().
[HI] Peek() (जिसे C++ STL में top() कहते हैं) स्टैक के शीर्ष तत्व का मान बिना उसे हटाए या top को बदले दिखाता है। खाली स्टैक पर stack[-1] पढ़ने की त्रुटि से बचने हेतु Peek में भी isEmpty() जांच आवश्यक है। ध्यान दें कि C++ std::stack में pop() कोई मान नहीं लौटाता (void), इसलिए पहले top() पढ़ना पड़ता है।
4. Helper Predicates (isEmpty, isFull) & Execution Tracing (सहायक जांच (isEmpty, isFull) और ट्रेसिंग अभ्यास)
[EN] isEmpty() evaluates (top == -1) and isFull() evaluates (top == MAX - 1), both returning a boolean in O(1) time. In dynamic linked stacks, isFull() is typically omitted or only checks heap allocation failure. Competitive exams frequently present a sequence of Push, Pop, and Peek calls starting from an empty stack and ask for the final top index, popped sequence, or Peek output.
[HI] isEmpty() (top == -1) और isFull() (top == MAX - 1) दोनों O(1) समय में बूलियन परिणाम देते हैं। डायनामिक लिंक्ड स्टैक में निश्चित क्षमता न होने से isFull() सामान्यतः नहीं होता। परीक्षाओं में प्रायः Push, Pop और Peek के क्रम देकर अंतिम top मान या आउटपुट पूछा जाता है।
Syntax, Formula & Technical Example (Stack Operations: Push, Pop, Peek, IsEmpty, IsFull)
int isEmpty(void) { return top == -1; }
int isFull(void) { return top == MAX - 1; }
void push(int x) {
if (isFull()) { printf("Overflow"); return; }
stack[++top] = x;
}
int pop(void) {
if (isEmpty()) { printf("Underflow"); return -1; }
return stack[top--];
}
Stack Operations: Push, Pop, Peek, IsEmpty, IsFull Quick Reference & Comparison Table
| Stack Operation | Pre-Condition & Action | Error / Return Behavior |
|---|---|---|
| push(item) | Checks !isFull(); sets stack[++top] = item | Triggers Stack Overflow if top == MAX - 1 |
| pop() | Checks !isEmpty(); returns stack[top--] | Triggers Stack Underflow if top == -1 |
| peek() / top() | Checks !isEmpty(); reads stack[top] unchanged | Does NOT decrement top or alter element count |
| isEmpty() / isFull() | Evaluates top == -1 / top == MAX - 1 | O(1) boolean guards used before Pop/Push |
Stack Operations: Push, Pop, Peek, IsEmpty, IsFull Top 20 Bilingual MCQs with Answers
Q1. Which error condition occurs when attempting to execute Push on a stack where top == MAX - 1?
(जब स्टैक में top == MAX - 1 हो और Push ऑपरेशन चलाया जाए, तो कौन सी त्रुटि उत्पन्न होती है?)
(A) Segmentation Null Return
(B) Stack Overflow
(C) Deadlock Exception
(D) Stack Underflow
Correct Answer: (B)
[EN] No unused capacity remains in the fixed array, so pushing another element causes Stack Overflow.
[HI] निश्चित आकार के स्टैक में कोई खाली स्थान शेष न रहने पर नया तत्व डालने से Stack Overflow होता है।
Q2. Which error condition occurs when calling Pop or Peek on an empty stack (top == -1)?
(खाली स्टैक (top == -1) पर Pop या Peek चलाने से कौन सी स्थिति उत्पन्न होती है?)
(A) Stack Overflow
(B) Buffer Reallocation
(C) Integer Wraparound
(D) Stack Underflow
Correct Answer: (D)
[EN] Trying to remove or inspect an element when zero elements exist triggers Stack Underflow.
[HI] जब स्टैक में कोई तत्व न हो और Pop या Peek चलाया जाए, तो उसे Stack Underflow कहा जाता है।
Q3. After executing: push(10), push(20), pop(), push(30), push(40), pop(), peek() on an initially empty stack, what value does peek() return?
(खाली स्टैक पर क्रमशः push(10), push(20), pop(), push(30), push(40), pop(), peek() चलाने पर peek() क्या मान लौटाएगा?)
(A) 30
(B) 20
(C) 40
(D) 10
Correct Answer: (A)
[EN] Trace: [10] -> [10,20] -> pop 20 -> [10] -> [10,30] -> [10,30,40] -> pop 40 -> [10,30]; peek() reads top = 30.
[HI] क्रम: 10, 20 पुश -> 20 पॉप -> 30, 40 पुश -> 40 पॉप; अंत में शीर्ष पर 30 बचता है जिसे peek() दिखाता है।
Q4. How does Peek differ from Pop on a non-empty stack?
(गैर-खाली स्टैक पर Peek ऑपरेशन Pop ऑपरेशन से किस प्रकार भिन्न है?)
(A) Peek takes O(n) time while Pop takes O(1)
(B) Peek deletes two elements at once
(C) Peek returns the top element without modifying top, whereas Pop removes it and decrements top
(D) Peek returns the bottom element while Pop returns the top element
Correct Answer: (C)
[EN] Peek is a non-destructive read-only inspection of stack[top]; Pop consumes the item and reduces stack size by 1.
[HI] Peek बिना कुछ हटाए केवल शीर्ष मान पढ़ता है (top नहीं बदलता), जबकि Pop तत्व को हटाकर top को 1 कम कर देता है।
Q5. If MAX = 4 and an empty stack runs: push(5), push(6), push(7), push(8), push(9), pop(), what is the value of 'top' at the end?
(यदि MAX = 4 हो और खाली स्टैक पर push(5), push(6), push(7), push(8), push(9), pop() चले (जहाँ Overflow पर push रुक जाता है), तो अंत में 'top' का मान क्या होगा?)
(A) 1
(B) 3
(C) 2
(D) 4
Correct Answer: (C)
[EN] 5, 6, 7, 8 fill indices 0..3 (top = 3); push(9) fails with Overflow; final pop() lowers top from 3 to 2.
[HI] 5, 6, 7, 8 क्रमशः इंडेक्स 0 से 3 भरते हैं (top=3); push(9) ओवरफ्लो से अस्वीकृत होता है; अंत में pop() से top घटकर 2 हो जाता है।
Q6. In C++ STL std::stack<int> s;, what does s.pop() return?
(C++ STL के std::stack<int> s में s.pop() का रिटर्न टाइप क्या होता है?)
(A) int (the popped integer value)
(B) void (it removes the top item without returning it)
(C) int* (pointer to popped node)
(D) bool (true if pop succeeded)
Correct Answer: (B)
[EN] C++ std::stack separates s.top() (access) and s.pop() (void removal) for strong exception safety.
[HI] C++ STL में एक्सेप्शन सुरक्षा के लिए s.pop() का रिटर्न टाइप void होता है; मान पढ़ने के लिए पहले s.top() कॉल करना पड़ता है।
Q7. Why does an array-based Pop operation NOT need to overwrite stack[top] with 0 before decrementing top?
(एरे-आधारित Pop में top घटाने से पहले stack[top] को 0 से साफ करना क्यों जरूरी नहीं होता?)
(A) Elements at indices > top are logically inaccessible and get overwritten on the next push
(B) Overwriting with 0 changes Pop complexity to O(n)
(C) C automatically sets popped array elements to NULL
(D) Array memory shrinks physically on every pop
Correct Answer: (A)
[EN] The variable 'top' defines the active stack boundary; anything beyond top is logically dead and costs O(1) to ignore.
[HI] स्टैक की सक्रिय सीमा केवल top द्वारा तय होती है; top के ऊपर बचा पुराना डेटा तार्किक रूप से अमान्य रहता है और अगले push पर अपने आप बदल जाता है।
Q8. What is the worst-case time complexity of all five operations—push, pop, peek, isEmpty, and isFull—in an array-based Stack?
(एरे-आधारित स्टैक में push, pop, peek, isEmpty और isFull सभी पांचों ऑपरेशन्स की सबसे खराब (Worst-case) टाइम कॉम्प्लेक्सिटी क्या है?)
(A) O(log n) for peek and O(1) for isEmpty
(B) O(1) each
(C) O(n) for push and O(1) for pop
(D) O(n) for all five operations
Correct Answer: (B)
[EN] Each operation performs a fixed constant number of index checks and array reads/writes regardless of stack size.
[HI] सभी पांचों ऑपरेशन्स केवल इंडेक्स जांच और सीधे एरे एक्सेस पर आधारित हैं, अतः प्रत्येक की टाइम कॉम्प्लेक्सिटी O(1) होती है।
Q9. What happens if you implement pop() as 'return stack[--top];' instead of 'return stack[top--];'?
(यदि pop() में गलती से 'return stack[top--];' की बजाय 'return stack[--top];' लिख दिया जाए, तो क्या होगा?)
(A) It works identically to stack[top--]
(B) It pops two elements properly
(C) It skips the actual top item and returns the element below it (or reads stack[-1] if 1 item existed)
(D) It causes Stack Overflow
Correct Answer: (C)
[EN] Pre-decrement lowers top first, returning stack[top - 1] instead of the current topmost element stack[top].
[HI] --top पहले इंडेक्स कम कर देता है, जिससे असली शीर्ष तत्व छूट जाता है और उसके नीचे वाला तत्व (या 1 तत्व होने पर stack[-1]) लौट आता है।
Q10. Given an empty stack and operations: push(2), push(4), peek(), pop(), push(6), peek(), pop(), pop(), isEmpty(). What does the final isEmpty() return?
(खाली स्टैक पर क्रमशः push(2), push(4), peek(), pop(), push(6), peek(), pop(), pop(), isEmpty() चलाने पर अंतिम isEmpty() क्या लौटाएगा?)
(A) Stack Underflow error
(B) 6
(C) false (0)
(D) true (1)
Correct Answer: (D)
[EN] 3 pushes (2, 4, 6) and 3 pops leave 0 items (peek() calls do not remove elements), so isEmpty() returns true.
[HI] कुल 3 बार push और 3 बार pop हुआ (peek तत्व नहीं हटाता), इसलिए अंत में स्टैक खाली होगा और isEmpty() सत्य (true) लौटाएगा।
Q11. If a stack starts empty, which sequence of operations triggers Stack Underflow?
(यदि कोई स्टैक प्रारंभ में खाली है, तो किस ऑपरेशन क्रम से Stack Underflow उत्पन्न होगा?)
(A) push(1), pop(), push(2), peek()
(B) push(1), push(2), peek(), pop()
(C) push(1), push(2), push(3), pop()
(D) push(1), push(2), pop(), pop(), pop()
Correct Answer: (D)
[EN] After 2 pushes and 2 pops the stack is empty; the 3rd pop() attempts to remove from an empty stack, triggering Underflow.
[HI] 2 पुश और 2 पॉप के बाद स्टैक खाली हो जाता है; तीसरा pop() खाली स्टैक पर चलने से Underflow दे देता है।
Q12. In Python using a built-in list 'st = []' as a stack, which methods map to Push and Peek?
(Python में लिस्ट 'st = []' को स्टैक की तरह उपयोग करते समय Push और Peek के लिए क्रमशः क्या लिखा जाता है?)
(A) st.append(x) for Push and st[-1] for Peek
(B) st.insert(0, x) for Push and st[0] for Peek
(C) st.add(x) for Push and st.pop(0) for Peek
(D) st.push(x) for Push and st.top() for Peek
Correct Answer: (A)
[EN] list.append(x) and list.pop() operate at the end of a Python dynamic array in O(1) amortized time, and st[-1] peeks.
[HI] Python लिस्ट के अंत में st.append(x) (Push), st.pop() (Pop) और st[-1] (Peek) O(1) अमॉर्टाइज्ड समय में काम करते हैं।
Q13. How many pops occur across the trace: push(A), push(B), pop(), push(C), push(D), pop(), pop(), push(E) — and what stays on top?
(push(A), push(B), pop(), push(C), push(D), pop(), pop(), push(E) के बाद शीर्ष (Top) पर कौन सा तत्व रहेगा?)
(A) E (with A directly below it)
(B) C (with A below it)
(C) D (with C below it)
(D) A alone
Correct Answer: (A)
[EN] Trace: [A] -> [A,B] -> [A] -> [A,C] -> [A,C,D] -> [A,C] -> [A] -> [A,E]. E sits on top above A.
[HI] प्रक्रिया: A -> A,B -> A -> A,C -> A,C,D -> A,C -> A -> अंत में E पुश होने पर [A, E] बचता है और शीर्ष पर E है।
Q14. Which condition allows you to safely compute the current free capacity remaining in a static array stack of size MAX?
(MAX आकार वाले स्टेटिक एरे स्टैक में शेष खाली स्थानों की संख्या किस सूत्र से प्राप्त होती है?)
(A) MAX - top
(B) MAX + top - 1
(C) top + 1
(D) (MAX - 1) - top
Correct Answer: (D)
[EN] Currently occupied slots = top + 1, so remaining free slots = MAX - (top + 1) = MAX - 1 - top.
[HI] भरे हुए स्थानों की संख्या (top + 1) होती है, इसलिए शेष खाली स्थान MAX - (top + 1) = (MAX - 1) - top होंगे।
Q15. Why is isFull() rarely needed in a Linked List implementation of a Stack?
(स्टैक के लिंक्ड लिस्ट कार्यान्वयन में isFull() ऑपरेशन की आवश्यकता सामान्यतः क्यों नहीं पड़ती?)
(A) Linked lists automatically overwrite the bottom node when full
(B) Linked stacks allocate nodes dynamically on the heap until system memory runs out
(C) Popping never frees memory in linked stacks
(D) Linked lists keep MAX capped at 100 nodes
Correct Answer: (B)
[EN] Unlike fixed arrays, linked stacks have no static MAX boundary; overflow only occurs if malloc/new fails.
[HI] लिंक्ड स्टैक में कोई निश्चित MAX सीमा नहीं होती; यह रनटाइम पर हीप मेमोरी मिलने तक लगातार बढ़ सकता है।
Q16. Which statement is NOT true regarding Stack operations?
(स्टैक ऑपरेशन्स के संबंध में निम्नलिखित में से कौन सा कथन असत्य (NOT true) है?)
(A) Executing Peek removes the topmost element and reduces stack size by one
(B) Pop raises Underflow if invoked on an empty stack
(C) Push increments top before storing the element when top starts at -1
(D) isEmpty returns true when top equals -1 in a 0-indexed array
Correct Answer: (A)
[EN] Peek strictly inspects the top value without deleting it or changing the stack size; only Pop removes elements.
[HI] Peek कभी भी शीर्ष तत्व को हटाता या स्टैक का आकार घटाता नहीं है; वह केवल मान दिखाता है।
Q17. Within Stack Operations: Push, Pop, Peek, IsEmpty, IsFull, which stack operation directly maps to: "Checks !isFull(); sets stack[++top] = item"?
(Stack Operations: Push, Pop, Peek, IsEmpty, IsFull में कौन सा Stack Operation सीधे "Checks !isFull(); sets stack[++top] = item" से संबंधित है?)
(A) push(item)
(B) peek() / top()
(C) isEmpty() / isFull()
(D) pop()
Correct Answer: (A)
[EN] In Stack Operations: Push, Pop, Peek, IsEmpty, IsFull, 'push(item)' handles 'Checks !isFull(); sets stack[++top] = item' (Triggers Stack Overflow if top == MAX - 1).
[HI] Stack Operations: Push, Pop, Peek, IsEmpty, IsFull में 'push(item)' का कार्य/विवरण 'Checks !isFull(); sets stack[++top] = item' (Triggers Stack Overflow if top == MAX - 1) है।
Q18. Which option accurately describes 'pop()' in Stack Operations: Push, Pop, Peek, IsEmpty, IsFull?
(Stack Operations: Push, Pop, Peek, IsEmpty, IsFull के संदर्भ में 'pop()' की सही विशेषता कौन सी है?)
(A) Checks !isEmpty(); reads stack[top] unchanged
(B) Checks !isFull(); sets stack[++top] = item
(C) Checks !isEmpty(); returns stack[top--]
(D) Evaluates top == -1 / top == MAX - 1
Correct Answer: (C)
[EN] 'pop()' provides 'Checks !isEmpty(); returns stack[top--]' — key point: Triggers Stack Underflow if top == -1.
[HI] 'pop()' का मुख्य विवरण 'Checks !isEmpty(); returns stack[top--]' है (Triggers Stack Underflow if top == -1)।
Q19. In Stack Operations: Push, Pop, Peek, IsEmpty, IsFull, identify the correct parameter for the specification: "Checks !isEmpty(); reads stack[top] unchanged".
(Stack Operations: Push, Pop, Peek, IsEmpty, IsFull में "Checks !isEmpty(); reads stack[top] unchanged" विवरण किस विकल्प के लिए सही है?)
(A) isEmpty() / isFull()
(B) pop()
(C) peek() / top()
(D) push(item)
Correct Answer: (C)
[EN] 'peek() / top()' corresponds to 'Checks !isEmpty(); reads stack[top] unchanged' (Does NOT decrement top or alter element count).
[HI] 'peek() / top()' का संबंध 'Checks !isEmpty(); reads stack[top] unchanged' (Does NOT decrement top or alter element count) से है।
Q20. Considering Stack Operations: Push, Pop, Peek, IsEmpty, IsFull, what key behavior defines 'isEmpty() / isFull()'?
(Stack Operations: Push, Pop, Peek, IsEmpty, IsFull में 'isEmpty() / isFull()' की मुख्य भूमिका या मान क्या है?)
(A) Checks !isEmpty(); reads stack[top] unchanged
(B) Checks !isFull(); sets stack[++top] = item
(C) Checks !isEmpty(); returns stack[top--]
(D) Evaluates top == -1 / top == MAX - 1
Correct Answer: (D)
[EN] 'isEmpty() / isFull()' represents 'Evaluates top == -1 / top == MAX - 1' (O(1) boolean guards used before Pop/Push).
[HI] 'isEmpty() / isFull()' का अर्थ/मान 'Evaluates top == -1 / top == MAX - 1' है (O(1) boolean guards used before Pop/Push)।
Stack Operations: Push, Pop, Peek, IsEmpty, IsFull — Exam FAQs
Q1: Why must Pop return an error or sentinel on Underflow instead of returning stack[-1]?
अंडरफ्लो होने पर Pop को stack[-1] लौटाने की बजाय त्रुटि या सेंटिनल क्यों देना चाहिए?
[EN] Index -1 lies outside the allocated array buffer in C/C++, causing undefined behavior, memory corruption, or reading unrelated variables.
[HI] C/C++ में stack[-1] एरे की सीमा के बाहर की अनधिकृत मेमोरी होती है, जिसे पढ़ने से कचरा मान या प्रोग्राम क्रैश हो सकता है।
Q2: In Python or Java, how is Stack Underflow handled?
Python या Java में Stack Underflow को कैसे संभाला जाता है?
[EN] Python raises IndexError on [].pop(), Java's Stack class throws EmptyStackException, and Deque.pop() throws NoSuchElementException.
[HI] Python खाली लिस्ट पर pop() करने पर IndexError देता है और Java का Stack क्लास EmptyStackException थ्रो करता है।
Q3: Does Peek change the time complexity if called repeatedly?
क्या बार-बार Peek कॉल करने से स्टैक की टाइम कॉम्प्लेक्सिटी पर कोई असर पड़ता है?
[EN] No; each Peek call reads stack[top] directly in O(1) constant time regardless of how many elements are inside.
[HI] नहीं; स्टैक में कितने भी तत्व हों, प्रत्येक Peek कॉल सीधे stack[top] को O(1) स्थिर समय में पढ़ती है।
Post a Comment
Post a Comment