Stack
Stack
1. Define Stack
A Stack is an ordered list in which all insertions (Push operation) and deletion (Pop
operation) are made at one end, called the top. The topmost element is pointed by top. The top is
initialized to -1 when the stack is created that is when the stack is empty. In a stack S = (a1,an), a1
is the bottom most element and element a is on top of element ai-1. Stack is also referred as Last
In First Out (LIFO) list.
A-B+C-D+
4. What are the postfix and prefix forms of the expression?
A+B*(C-D)/(P-R)
push() O(1)
pop() O(1)
isEmpty() O(1)
isFull() O(1)
6. Applications of Stack
Expression Evaluation and Conversion:
Stacks are used to evaluate and convert expressions between infix, prefix, and postfix notations.
Backtracking:
Stacks are useful in backtracking algorithms, allowing the program to return to a previous state.
Function Calls:
Stacks manage function calls by storing return addresses and local variables.
Parenthesis Checking:
Stacks can efficiently verify the correct matching and nesting of parentheses in expressions.
String Reversal:
Stacks facilitate reversing strings by pushing characters onto the stack and then popping them.
Syntax Parsing:
Compilers and interpreters use stacks for parsing the syntax of programming languages.
Memory Management:
Stacks assist in memory allocation and deallocation in some systems.