Stack and Queue
Stack and Queue
• https://
www.youtube.com/watch?v=d_XvFOkQz5k
Evaluating arithmetic
expressions
• Polish notation(Prefix) +AB
• Reverse polish notation(Postfix ) AB+
• Infix notation A+B
• A+B/*C-
• (A+B)/(C-D)
• +AB-C
• http://www.btechsmartclass.com
/data_structures/infix-to-postfix.
Conversion of infix to postfix notation
Conversion of infix to postfix
• Place parentheses around every group of operators in the
correct order of evaluation. There should be one set of
parentheses for every operator in the infix expression
• ((A * B) + (C / D))
• For each set of parentheses, move the operator from the
middle to the end preceding the corresponding closing
parenthesis.
• ((A B *) (C D /) +)
• Remove all of the parentheses, resulting in the equivalent
postfix expression.
• AB*CD/+
Evaluating arithmetic
expressions
1) Postfix expression: 2 3 4 * +
Input Stack
234*+ empty Push 2
34*+ 2 Push 3
4*+ 32 Push 4
*+ 432 Pop 4 and 3, and
perform 4*3 = 12.
Push 12 into the stack.
Input Stack
4*25*+ 3 Push 4
*2 5 * + 43 Pop 3 and 4 from the stack and
perform 3*4 = 12. Push 12 into
the stack.
25*+ 12 Push 2
5*+ 2 12 Push 5
*+ 5 2 12 Pop 5 and 2 from the stack and
perform 5*2 = 10. Push 10 into
the stack.
Types:
1)Bounded priority queue - small limited range of p priorities
over the interval of integers [0 . . . P]
2)Unbounded priority queue - no limit on the range of integer
values
Doubly Ended queue(Deque)
• It allows insertion and removal of elements from
both the ends, i.e , front and back.
Applications of Queue
• In operating systems like Semaphores,FCFS, Buffer for
devices
• CPU task scheduling
• In networks like mail queues, queues in routers/switches