![]() |
VOOZH | about |
A stack follows the Last In, First Out (LIFO) principle, meaning the last element added is the first to be removed. Stacks are a fundamental data structure used in many real-world applications, including expression evaluation, function call management, and backtracking algorithms.
Insert at Bottom: The time complexity of inserting an element at the bottom of a stack is O(n), as all the elements need to be popped off and pushed back onto the stack.
The common operations on a stack are push (insert an element), pop (remove the top element), and peek (view the top element).
A stack can be implemented using an array by maintaining a pointer to the top of the stack.
Push, pop, and peek operations have a time complexity of O(1). Please remember in case of linked list implementation and a fixed sized array implementation, the time complexity in the worst case is O(1), However if we use dynamic sized array and allow the stack to grow, then the amortized time complexity [Average over n operations] is O(1).
Stacks follow the LIFO (Last-In, First-Out) principle, which matches function call behavior:
Stacks are used in various applications, such as function calls, recursion, expression evaluation, and parsing.
A stack overflow occurs when the stack exceeds its allocated memory.
A stack underflow occurs when the stack is empty and an attempt is made to pop an element.
The idea is to use one queue for normal insertion (push operation) and the second queue for reversal of elements to mimic the stack's pop operation. The goal is to make the pop operation efficient by adjusting the order of elements between the two queues.
A postfix expression is an expression where the operator follows the operands. To evaluate a postfix expression using a stack, you push operands onto the stack and, when encountering an operator, pop the required operands from the stack, perform the operation, and push the result back onto the stack.
A prefix expression is an expression where the operator precedes the operands. To evaluate a prefix expression using a stack, you traverse the expression from right to left, pushing operands onto the stack. When an operator is encountered, you pop the required operands from the stack, perform the operation, and push the result back onto the stack.
A stack can be implemented using a linked list by using the head of the list as the top of the stack. In this implementation:
To convert an infix expression to postfix using a stack, we use an algorithm that processes each character of the expression, applying operator precedence rules and using the stack to hold operators temporarily.
Time Complexity: O(n), where n is the length of the infix expression. Each character is processed once, and each operation (push or pop) takes constant time.
To convert an infix expression to a prefix expression, we use a similar algorithm as for postfix conversion but traverse the expression from right to left.
Time Complexity: O(n), where n is the length of the infix expression. Reversing the expression takes O(n), converting it to postfix takes O(n), and the final reverse also takes O(n).
A stack can be used to check if a parenthesis expression is balanced by following these steps:
Please note that if there is only one type of bracket, then we do not need stack. We can solve the problem using a count variable. However for multiples types of brackets a stack is needed to solve.
The following list of 50 stack coding problems covers a range of difficulty levels, from easy to hard, to help candidates prepare for interviews.
Top 50 Problems on Stack Data Structure asked in SDE Interviews