Data Structures Using C: Stacks and Queues
Data Structures Using C: Stacks and Queues
Data Structures Using C: Stacks and Queues
Module 2
STACKS AND QUEUES
1
Amity School of Engineering and Technology
Contents
Applications of Stack:
Infix to Postfix Conversion
Postfix Evaluation
2
Amity School of Engineering and Technology
Learning Objectives
• Impart in-depth knowledge of data structure and its implementation
in computer programs.
• Make students understand the concepts of Stack linear data structure.
• Make students understand the applications of Stack.
3
Amity School of Engineering and Technology
Recommended Reading
Textbooks:
• Yashwant Kanetkar,”Data Structure using C”, BPB Publication, 5th Edition ,2011
• A.Tannenbaum,Y. Lanhgsam and A.J. Augenstein ,” Data Structures Using C And C++ “,Prentice Hall of
India,2nd Edition,2009.
• Jean-Paul Tremblay, P.G Sorenson, “An Introduction to Data Structures with applications”, Mcgraw-
Hill ,2nd Edition ,1984.
Reference Book:
• Robert L Kruse, “Data Structure and Program Design in C”, Prentice Hall (1991).
• Noel Kalicharan ,“Data Structure in C” ,Ist Edition Create space publisher, 2008.
• Mark Allen Weiss,“Data Structure and algorithm Analysis in C”,2nd Edition AddisonWesley,1996.
• E. Balagurusamy, “Problem Solving through C language”, TMH publication, Fourth Edition, 2008.
• R.S Salaria ,“Data Structures & Algorithms using C”,Khanna Publication,4th Edition,2009
• E.Horowitz and S.Sahni,”Fundamentals of Data Structures in C “,2nd Edition, Universities Press,2008.
4
Amity School of Engineering and Technology
Module Assessment
• Quiz (Conceptual and Numerical Based)
• Assignment
5
Expression
An expression is a collection of operators and operands that represents a specific value.
Based on the operator position, expressions are divided into three categories. They are
as follows...
• Infix Expression
• Postfix Expression (Reverse Polish)
• Prefix Expression (Polish)
6
Types of Expression
Infix Expression
•Operator is used in between operands.
Operand1 Operator Operand2
•Example : a+b
Postfix Expression
•Operatoris used after operands.
Operand1 Operand2 Operator
•Example : ab+
Prefix Expression
•operatoris used in between operands.
Operator Operand1 Operand2
•Example : +ab
7
INFIX TO POSTFIX ALGORITHM
Read all the symbols one by one from left to right in the given Infix Expression.
1. Push ‘(‘ onto Stack and ‘)’ at the end of the infix expression.
2. If the symbol is an operand, then directly add it to final postfix expression(Output).
3. If the reading symbol is left parenthesis '(', then Push it on to the Stack.
4. If the reading symbol is right parenthesis ')', then pop all the symbols from the stack until a left parenthesis
appears. Discard the left parenthesis and add remaining popped symbols to the postfix expression in the
order in which they are popped.
5. If the reading symbol is an operator (+ , - , * , / etc.,), then Check, if the operator on the top of the stack has
higher or equal precedence than the one being read, pop the operator and add it to the postfix expression.
Repeat the process until a lower precedence operator appears at the top of the stack. Then push the
current operator onto the stack.
8
Infix to Postfix
Infix Expression= A*B+C
9
Quiz 2
1. A-(B*C/D^F) +G *H/I-J
2. (A-2*(B+C)/D*E)+F
3. A+B*C/D-F+A^E
10
Evaluation of Postfix
Read all the symbols one by one from left to right in the given Postfix Expression.
•If the reading symbol is operator (+ , - , * , / etc.,), then perform two pop operations
and store the two popped operands in two different variables (operand1( stack top
symbol) and operand2( next-to-top symbol)). Then perform reading symbol operation
using operand2 operator operand1 and push result back on to the Stack.
11
Example: A B * C +
Reading Symbol Stack operation Stack Content
A Push A
B Push A,B
* Operand1=B (A*B)
Operand2=A
A*B
C Push (A*B), C
12
Example: Evaluate (5+6*3)+(5+6 )*3 using Postfix Notation
14
Postfix Evaluation of Expression: 5 6 3 * + 5 6 + 3 * +
15
Practice Question
Evaluate the following expression using postfix notation.
1) 5*2+(16/2^3-3*2)+14/7
2) 120-7^3/49+(5*2-2^3)-40
16
17