Stack
1. Define stack. What are the basic operations?
Answer:
A stack is a linear data structure that follows the LIFO (Last In First Out) principle.
The main operations are:
• push() – Add an element to the top
• pop() – Remove the top element
• peek() or top() – View the top element without removing it
• isEmpty() – Check if the stack is empty
2. Differentiate between Stack and Queue
Feature Stack (LIFO) Queue (FIFO)
Insertion At the top At the rear
Deletion From the top From the front
Principle Last In First Out First In First Out
Use cases Undo, recursion Scheduling, buffers
3. What is LIFO?
Answer:
LIFO stands for Last In First Out.
In this system, the element that was added last is the one that is removed first.
Example: A pile of books — the book placed last is removed first.
4. Applications of Stack
• Function call management (call stack)
• Undo/redo in text editors
, • Syntax parsing (HTML/XML)
• Backtracking in games or maze problems
• Expression evaluation (postfix/infix)
5. Stack Overflow and Underflow
• Overflow: When the stack is full and a push() is attempted.
• Underflow: When the stack is empty and a pop() is attempted.
6. Algorithms for Push and Pop (Array)
Push(x):
arduino
CopyEdit
if top == size - 1:
print("Stack Overflow")
else:
top = top + 1
stack[top] = x
Pop():
arduino
CopyEdit
if top == -1:
print("Stack Underflow")
else:
x = stack[top]
top = top - 1
return x
7. Check Balanced Parentheses Using Stack
Logic: