Stack and Queue
Stack & Queue in DSA (Java)
Understand LIFO & FIFO structures for efficient problem solving.
๐ Topics Covered
๐ Stack (LIFO)
Stack follows Last In First Out principle.
๐ก Last inserted element is removed first.
Operations
- Push
- Pop
- Peek
- isEmpty
Applications
- Expression evaluation
- Undo/Redo
- Function call stack
๐ถ Queue (FIFO)
Queue follows First In First Out principle.
๐ก First inserted element is removed first.
Operations
- Enqueue
- Dequeue
- Front
Applications
- Process scheduling
- Printer queue
- BFS traversal
☕ Java Implementation
class Stack {
int[] arr;
int top = -1;
void push(int x) { arr[++top] = x; }
int pop() { return arr[top--]; }
}
๐งช Important Questions
- Stack vs Queue difference
- Implement stack using queue
- Time complexity (O(1))
- Applications in real-world
๐ฏ Conclusion
Stacks and queues are fundamental data structures used in many real-world systems and interview problems.