Stack can be FILO - First in last out or LIFO - First in last out .
Underflow condition - When a pop operation is performed on an empty stack
Peek Operation : get the topmost item
Stack can be implemented using an array or a Linked List
Basic ADT : Using arrays :
struct Stack {
int top ;
unsigned int capacity ;
int* array ;
}
Disadvantages using array :
No dynamic size
Doesnt grow or shrink using depending on needs at runtime
Using Linked List :
struct StackNode {
int data ;
struct StackNode* next ;
}
Balancing of symbols:
Infix to Postfix/Prefix conversion
Redo-undo features at many places like editors, photoshop.
Forward and backward feature in web browsers
Used in many algorithms like Tower of Hanoi, tree traversals.
Other applications can be Backtracking, Knight tour problem, rat in a maze, N queen problem and sudoku solver.
We need postfix notations because compiler scans the expressions from left to right or from right to left
Consider the below expression: a op1 b op2 c op3 d
If op1 = +, op2 = *, op3 = +
The compiler first scans the expression to evaluate the expression b * c, then again scan the expression to add a to it. The result is then added to d after another scan.
The expressions written in postfix form are evaluated faster compared to infix notation as parenthesis are not required in postfix.
No comments:
Post a Comment