⚡0 XP🔥0

Ch 6 · Data Structure - Stack | डेटा संरचना - स्टैक

Lesson 6 of 10 ⏱ 3 min read ✎ 1 exercises ❓ 5-question quiz ▶ Runs Python

What a data structure is, the stack (LIFO), push and pop operations, overflow and underflow, and implementing a stack using a Python list with applications.

Stack#

A stack is a linear data structure that follows LIFO - Last In, First Out - like a pile of plates. Insertion (push) and deletion (pop) happen at one end called the top.

  • Overflow: pushing into a full stack. Underflow: popping from an empty stack.
  • Applications: undo in editors, back button in browsers, reversing a string, checking balanced brackets, function calls, converting/evaluating expressions.
हिंदी में समझें

स्टैक LIFO (अंत में आया, पहले गया) सिद्धांत पर काम करता है, जैसे प्लेटों का ढेर। push से ऊपर (top) पर तत्व जोड़ते हैं और pop से ऊपर से हटाते हैं। खाली स्टैक से pop करना अंडरफ़्लो कहलाता है।

Stack using a list#

▶ Try it YourselfPython
Ctrl + Enter to run

Application - reverse a string#

▶ Try it YourselfPython
Ctrl + Enter to run

Typical board question#

Push all numbers greater than 50 from a list into a stack, then pop and display them until the stack is empty, printing "Stack Empty" at the end.

Exam tips#

  • Always check for an empty stack before pop().
  • Know that list.append() = push and list.pop() = pop.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Push numbers greater than 50

Given list N, push every number greater than 50 onto stack. Then pop and print each element (one per line) until the stack is empty, and finally print Stack Empty.

Expected output:
99
51
75
Stack Empty

Quick Quiz

Q1.

Stack follows

Q2.

Removing an element from an empty stack is

Q3.

In Python, push is implemented using

Q4.

Which is an application of stack?

Q5.

After push 1, push 2, push 3, pop - the top is