Assignment # 2
Stack & Queue
Problem 1: Implement a stack class in Python with the following methods:
• push(element): Adds an element to the top
• pop(): Removes and returns the top element
• peek(): Returns the top element without removing it
• isEmpty(): Checks if the stack is empty
• size(): Returns the number of elements
Problem 2: Write a function that uses a stack to check if a string containing
parentheses (), brackets [], and braces {} is balanced.
Example:
• Input: "({[]})" → Output: True
• Input: "({[)]}" → Output: False
Problem 3: Implement a stack-based calculator that evaluates postfix (Reverse Polish
Notation) expressions.
Example:
• Input: "3 4 + 2 *" → Output: 14
• Input: "5 1 2 + 4 * + 3 -" → Output: 14
Problem 4: Implement a queue class with the following methods:
• enqueue(element): Adds an element to the rear
• dequeue(): Removes and returns the front element
• front(): Returns the front element without removing it
• isEmpty(): Checks if the queue is empty
• size(): Returns the number of elements
Problem 5: Implement a circular queue with fixed capacity that efficiently utilizes
memory:
• Handle wrap-around conditions
• Implement proper full/empty checks
• Include a method to display all elements