0% found this document useful (0 votes)
130 views

Unit 4 Stack and Queues

Stacks are linear data structures that follow LIFO (last in, first out) ordering. Items can only be inserted and removed from one end of the stack. Stacks are commonly used to postpone processing steps until other conditions are met. Queues follow FIFO (first in, first out) ordering. Recursion involves functions that call themselves during their execution. Examples of recursive functions include calculating factorials and generating the Fibonacci sequence.

Uploaded by

Paresh Sawant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

Unit 4 Stack and Queues

Stacks are linear data structures that follow LIFO (last in, first out) ordering. Items can only be inserted and removed from one end of the stack. Stacks are commonly used to postpone processing steps until other conditions are met. Queues follow FIFO (first in, first out) ordering. Recursion involves functions that call themselves during their execution. Examples of recursive functions include calculating factorials and generating the Fibonacci sequence.

Uploaded by

Paresh Sawant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 79

Stacks, Queues, Recursion

Stacks
• What is a Stack?
– A stack is a linear structure in which items can be
inserted and removed only at one end.
• What can we do with a stack?
o push – insert an item into the stack
o pop – Look at the item on top of the stack and
remove it
E top
D top D D
C top C C C
B top B B B B
A top A A A A A
Postponed Decisions
• Stacks are frequently used to indicate the
order of processing of data when certain steps
of processing must be postponed until other
conditions are fulfilled .
C top
B top B B top
top A A A A top
A
Array Representation of Stacks
• Linear array
• One way list
Linked Representation of Stacks
Arithmetic Expressions
• Precedence of operations

Assume that Any parenthesis free operations on the same level are
performed from left to right
Polish Notations
• Polish notation, named after the Polish
mathematician Jan Lukasiewicz, refers to the
notation in which operator symbol is placed
before its two operands e.g.
• +AB
• -CD
• *EF
• /GH
In POSTFIX Notation, An OPERATOR is
placed IMMEDIATELY AFTER its
OPERANDS.

INFIX POSTFIX
a+b ab+
a+b*c abc*+
a*b+c ab*c+
(a + b) * c ab+c*
Building an Arithmetic Expression Evaluator

Infix and Postfix Expressions:

Infix Expression Properties:


- Usual precedence and associativity of operators
- Parentheses used to subvert precedence
Postfix Expression Properties:
- Both operands of binary operators precede operator
- Parentheses no longer needed

Infix Expression Equivalent Postfix Expression


3*4+5 34*5+
3*(4+5)/2 345+*2/
(3+4)/(5-2) 34+52-/
7-(2*3+5)*(8-4/2) 723*5+842/-*-
3-2+1 32-1+
Infix to postfix expressions
FPE Infix to Postfix
(((A+B)*(C-E))/(F+G))

stack:<empty>
output: []
FPE Infix to Postfix
((A+B)*(C-E))/(F+G))

stack:(
output: []
FPE Infix to Postfix
(A+B)*(C-E))/(F+G))

stack:((
output: []
FPE Infix to Postfix
A+B)*(C-E))/(F+G))

stack:(((
output: []
FPE Infix to Postfix
+B)*(C-E))/(F+G))

stack:(((
output: [A]
FPE Infix to Postfix
B)*(C-E))/(F+G))

stack:(((+
output: [A]
FPE Infix to Postfix
)*(C-E))/(F+G))

stack:(((+
output: [A B]
FPE Infix to Postfix
*(C-E))/(F+G))

stack:((
output: [A B + ]
FPE Infix to Postfix
(C-E))/(F+G))

stack:((*
output: [A B + ]
FPE Infix to Postfix
C-E))/(F+G))

stack:((*(
output: [A B + ]
FPE Infix to Postfix
-E))/(F+G))

stack:((*(
output: [A B + C ]
FPE Infix to Postfix
E))/(F+G))

stack:((*(-
output: [A B + C ]
FPE Infix to Postfix
))/(F+G))

stack:((*(-
output: [A B + C E ]
FPE Infix to Postfix
)/(F+G))

stack:((*
output: [A B + C E - ]
FPE Infix to Postfix
/(F+G))

stack:(
output: [A B + C E - * ]
FPE Infix to Postfix
(F+G))

stack:(/
output: [A B + C E - * ]
FPE Infix to Postfix
F+G))

stack:(/(
output: [A B + C E - * ]
FPE Infix to Postfix
+G))

stack:(/(
output: [A B + C E - * F ]
FPE Infix to Postfix
G))

stack:(/(+
output: [A B + C E - * F ]
FPE Infix to Postfix
))

stack:(/(+
output: [A B + C E - * F G ]
FPE Infix to Postfix
)

stack:(/
output: [A B + C E - * F G + ]
FPE Infix to Postfix

stack:<empty>
output: [A B + C E - * F G + / ]
Evaluation of Postfix Expression
• P : 5, 6, 2, +, *, 12, 4, /, -, )  This is Sentinel at end of P
• STACK : 5
5 6
5 6 2
5 8
40
40 12
40 12 4
40 3
37  Result of Postfix expression
Let's see an example to better understand the algorithm:
Expression: 456*+
Let's see an example to better understand the algorithm:
Expression: 456*+
Examples:

Infix Expression Prefix Expression Postfix Expression


A+B*C+D ++A*BCD ABC*+D+

(A + B) * (C + D) *+AB+CD AB+CD+*

A*B+C*D +*AB*CD AB*CD*+

A+B+C+D +++ABCD AB+C+D+


Evaluation of Prefix Expression using Stack:
For solving prefix expression using Stack certain steps are to be
followed. The steps are :

1) Reverse the given Infix expression.

2) Make every open bracket as close bracket and vice-versa.

3) Convert expression into postfix form.

4) Reverse the expression.

Receive expression is Prefix expression.


Evaluation of Prefix Expression using Stack:

Eg. (A +(B *(C ^(D ^E))))

Step:1 Reverse the expression ))))E^D(^C(*B(+A(

Step:2 Change brackets ((((E ^ D)^ C)* B)+ A)


Evaluation of Prefix Expression using Stack:
Scanned Symbol STACK Expression
( (
( ((
( (((
( ((((
E (((( E
^ ((((^ E
D ((((^ ED
) ((( ED^
^ (((^ ED^
C (((^ ED^C
) (( ED^C^
* ((* ED^C^
B ((* ED^C^B
) ( ED^C^B*
+ (+ ED^C^B*
A (+ ED^C^B*A
) Empty ED^C^B*A+
Evaluation of Prefix Expression using Stack:

Now reverse the obtained expression i.e ED^C^B*A+

Prefix expression is: + A * B ^ C ^ DE


Evaluation of Postfix Expression using Stack:
For solving postfix expression using Stack certain steps are to be followed. The steps
are :
1. Add a left parenthesis '(' at start of expression and right parenthesis ')' at end of
expression.
2. Initially push any symbol to the stack and consider it a lower precedence.

3. Each operand simply add to the expression(Postfix) and does not change the state
of the stack.

4. If any higher or equal precedence operator is in the stack, and next we get any
lower preference operator then we pop that higher preference operator and push
lower preference operator onto the stack.

5. In any parenthesis expression if there is left parenthesis in the stack then we only
remove this parenthesis when a right parenthesis of a same level encounter.

6. Exit
Evaluation of Postfix Expression using Stack:
1) A + (B * C - (D / E ^ F) * G) * H

In this example, we scanned symbol from left to right and put


operators and brackets in stack and operands in Expression. In
line 8 when we encounter '-' operator having precedence less
than operator '*'(which is in stack) then pop '*' form stack and
push it to expression. Similarly when we encounter ')' then pop
all operators till '(' and push it to Expression.
Evaluation of Postfix Expression using Stack:
1) A + (B * C - (D / E ^ F) * G) * H
Scanned Symbol STACK Postfix Expression
( (
A ( A
+ (+ A
( (+( A
B (+( AB
* (+(* AB
C (+(*  ABC
 - (+(- ABC*
( (+(-( ABC*
D (+(-( ABC*D
/ (+(-( / ABC*D
E (+(- ( /  ABC*DE
 ^ (+(-(/^ ABC*DE
F (+(-(/^ ABC*DEF
) (+(- ABC*DEF^/
* (+(-* ABC*DEF^/
G (+(-* ABC*DEF^/G
) (+ ABC*DEF^/G*-
* (+* ABC*DEF^/G*-
H (+* ABC*DEF^/G*-H
) Empty ABC*DEF^/G*-H*+
Evaluation of Postfix Expression using Stack:
1) A + (B * C - (D / E ^ F) * G) * H

Postfix Expression for above infix is : ABC * DEF ^/ G *- H *+

E.g. (5 * ( 6 + 2) - (12 / 4))

Postfix Expression for above infix : 5, 6, 2, +, *, 12, 4, /, -


Quicksort: An Application of Stacks
• Quick sort is an algorithm of the divide and conquer type.
Factorial Function

For example:
5! = 5 * 4 * 3 * 2 * 1 = 120
The usual definition of “n factorial” is

“n! = n * (n — 1) * (n — 2) *…* 2 * 1,”

where ’n’ is a positive integer.


Factorial Function
Fibonacci Sequence
Fibonacci series is defined as a
sequence of numbers in which the first
two numbers are 1 and 1, or 0 and 1, depending on
the selected beginning point of the sequence, and each
subsequent number is the sum of the previous two.
Fibonacci Sequence
First Term = 0
Second term = 1
Third Term = First + Second = 0+1 =1
Fourth term = Second + Third =1+1 = 2
Fifth Term = Third + Fourth = 2+1 = 3
Sixth Term= Fourth + Fifth = 3+2 = 5
Seventh Term = Fifth + Sixth = 3+5 = 8
Eighth Term = Sixth + Seventh = 5+8 = 13 … and so
on to infinity!
Fibonacci Sequence
1. Start
2. Declare variables i, a,b , show
3. Initialize the variables, a=0, b=1, and show =0
4. Enter the number of terms of Fibonacci series to be
printed
5. Print First two terms of series
6. Use loop for the following steps
-> show=a+b
-> a=b
-> b=show
-> increase value of i each time by 1
-> print the value of show
7. End
Queues
• A queue is a linear list of elements in which
deletion can take place only at one end called
front and insertion can take place only at the
other end called rear.
• Queues are also called as FIFO list.
Array Representation of Queue
Linked Representation of Queue
DEQUES
• Deque is a linear list in which elements can be
added or removed at either end but not in the
middle.
Types of Deque
• Input Restricted Deque :
– It is a Deque which allows insertion at only one
end of the list but allows deletions at both ends of
the list.
• Output Restricted Deque:
– It is a Deque which allows deletion at only one end
of the list but allows insertions at both ends of the
list.
Priority Queues
• It is a collection of elements such a that each
element has been assigned a priority, such that, the
order in which elements are deleted and processed
comes from the following rules.
– An elements of higher priority is processed before an
element of lower priority.
– Two elements with the same priority are processed
according to the order in which they were added to the
queue.
One way list representation of a
priority queue
Deletion of An ITEM in to Priority Queue

1. Set ITEM := INFO[START]


2. Set START := LINK[START] (Delete first
node from the list)
3. Process ITEM
4. Exit
Insertion of An ITEM in to Priority Queue
Insertion Algorithm

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy