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#
Application - reverse a string#
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 andlist.pop()= pop.