Download presentation
Presentation is loading. Please wait.
1
BCA II Data Structure Using C
Submitted By : Veenu Saini Department of Information Technology
2
Applications of Stacks
Matching Balancing Parenthesis Evaluating an postfix expression Infix to postfix expression Applications of Stacks
3
Matching Balancing Parenthesis
Given an expression of ‘(‘ and ‘)’, a ‘(‘ must match with a ‘)’, or else it is illegal. ( )( ), ( ( ( ) ) ), ( ( ( ) ) ( ) ) are all legal, while ( ( ) (, ) ( ) ( are all illegal. Obviously, counting the numbers of ‘(‘ and ‘)’ in the expression is not enough. Applications of Stacks
4
Matching Balancing Parenthesis
Insights By convention, the expression is reading from left-to-right. A ‘)’ is matched with the closest, unmatched ‘(‘ on its left. For example, ( ( ( ) ) ( ) ) Applications of Stacks
5
Matching Balancing Parenthesis
Suppose we have a ‘)’. How do we know which ‘(’ is closest and unmatched? If we read the expression from left-to-right, the MOST RECENTLY UNMATCHED ‘(’ is cancelled with ‘)’. How can we keep track of the MOST RECENTLY READ (LAST) ‘(’ ? (keep in mind there are many pending unmatched ‘(’). Which data structure is keeping track of the most recent item ? Stack LIFO structure Applications of Stacks
6
Matching Balancing Parenthesis
When we see a ‘(‘, we push it into a stack. When we see a ‘)’, we pop ‘(‘ from the stack. This ‘(‘ matches with current ‘)’. What happen if the stack is empty (meaning there is no such ‘(’) at that moment ? What happen if we have finished reading the expression, but the stack is not empty ? It means there are more ‘(‘ than ‘)’ in the expression. It means the input is illegal. It means the input is illegal. Applications of Stacks
7
Infix to Postfix Expressions
Infix expression Suppose the expression only has operators ‘*’ and ‘+’; and ‘*’ has a higher precedence than ‘+’. So, = 10, and 1+2*4=9, etc. The expression may also have parenthesis to complicate the problem, i.e., (1+2)*4=12 (1+2*5+1)*3=36. (1+2*(5+1))*3=39. Applications of Stacks
8
Infix to Postfix Expressions
1 3 + 1 2 4 * + * * * are all postfix expressions. No ‘(‘, ‘)’ is in the expression. To evaluate a postfix expression, we need a stack. Applications of Stacks
9
Infix to Postfix Expressions
Evaluate a postfix expression. Read the expression from left-to-right. When a number is seen, it is pushed onto the stack. When an operator is seen, the operator is applied to the two numbers that are popped from the stack. The result is pushed on the stack. Applications of Stacks
10
Infix to Postfix Expressions
Example : * * Applications of Stacks
11
Infix to Postfix Expressions
How to convert an infix expression to a postfix expression? Use a stack. Read the infix expression from left-to-right. When an operand (number) is read, output it. If an operator (other than ‘(‘, ‘)’) is read, pop the stack (and output the operator) until a lower precedence operator or ‘(‘ is on the top of stack. Then push the current operator on the stack. If ‘(‘ is read, push it on the stack. If a ‘)‘ is read, pop the stacks (and output the operators), until we meet ‘(’. Pop that ‘(’ (do no output it). Finally, if we read the end of the expression, pop the stack (and output the operators) until the stack is empty. Applications of Stacks
12
Infix to Postfix Expressions
Example : (1+2*(5+1))*3 postfix expression * + 3 * Applications of Stacks
13
Application of Queue Real life examples
Waiting in line Waiting on hold for tech support Applications related to Computer Science Threads Job scheduling (e.g. Round-Robin algorithm for CPU allocation) Applications of queue
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.