Data Structures: Stacks Lecture 5,6
Data Structures: Stacks Lecture 5,6
Stacks
Lecture 5,6
• Validation
1. There is an equal number of right and left parentheses
2. Every right parenthesis is preceded by a matching left
parenthesis
• (A*B)
• 11110 Right
• A*B(
• 0001 Error
( ( (
{ { { { {
1 2 3 4 5 6 7
FUIEMS Malik Imran Daud
Another implementation of
Stack
• Stacks can be declared by using structures containing
two objects
struct stack s;
x=pop(&s);
A+B*C
A+(B*C) Parentheses for emphasis
A+(BC*) Convert the multiplication,Let D=BC*
A+D Convert the addition
A(D)+
ABC*+ Postfix Form
2-3*4+5 234*-5+ -5
(2 - 3) * (4 + 5) 23-45+* -9
5) If it is a closing parenthesis, pop operators from stack and output them until
an opening parenthesis is encountered. pop and discard the opening
parenthesis.
6) If there is more input go to step 1
7) If there is no more input, pop the remaining operators to output.