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

Dsa CH 4 Stacks Application

Uploaded by

Ayush Sinha
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)
45 views

Dsa CH 4 Stacks Application

Uploaded by

Ayush Sinha
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/ 32

STACKS APPLICATIONS

Quick Introduction
 Stacks are linear data structures.
 A stack is a particular kind of abstract data type or
collection in which the only operations on the collection
are the addition of an entity to the collection, known as
push and removal of an entity, known as pop
 All deletions and insertions occur at one end of the
stack known as the TOP.
 Data going into the stack first, leaves out last.
 Stacks are also known as LIFO data structures (Last-In,
First-Out).
Basic Stack Operations
 push – Adds an item to the top of a stack.

 pop – Removes an item from the top of the stack


and returns it to the user.

 stack top (top, peek) – Copies the top item of the


stack and returns it to the user; the item is not
removed, hence the stack is not altered.
Working of a Stack

Top= -1 Top= 0 Top= 1 Top= 2 Top= 1


Stack Implementation
 Stacks structures are usually implemented using
arrays or linked lists.
 For both implementations, the running time is O(n).
 Running time for PUSH operation is O(1)
 Running time for POP operation is O(1)
 Running time for TOP operation is O(1)
Stack Applications
 Reversing Data: We can use stacks to reverse data.
(example: files, strings)
Very useful for finding palindromes.

Consider the following pseudocode:


1) read (data)
2) loop (data not EOF and stack not full)
1) push (data)
2) read (data)
3) Loop (while stack notEmpty)
1) pop (data)
2) print (data)
Stack Applications
 Evaluating arithmetic expressions.

 Prefix: + a b
 Infix: a + b (what we use in grammar school)
 Postfix: a b +

 In high level languages, infix notation cannot be used to


evaluate expressions.
 We must analyze the expression to determine the order in
which we evaluate it.
 A common technique is to convert a infix notation into postfix
notation, then evaluating it.
Infix to Postfix Conversion
1. Scan the expression from left to right for each
character.
2. If the input is an operand then place it in the output
string
3. If the input is operator then push it into the stack.
❖ If the input operator is having highest priority than
stack top operator then push it into the stack.

❖ If the input operator is having lowest or equal priority


then pop the stack operator and place it in the output
string and push the input character to the stack.
Infix to Postfix Conversion
4. If the input is ‘(‘ parenthesis then push all the
operators of input into the stack until ‘)’ parenthesis
occurs.

5. If the input is ‘)’ then pop all the operators until


‘(‘and place it in the output string and discard the
parenthesis.

6. If the expression is completely scanned then pop


all the operators from stack and add it to output
string.
Infix to Postfix Example
A + B * C - D / E
Infix to Postfix Example
A + B * C - D / E
Input String Stack Output Stack
A+B*C-D/E A
A+B*C-D/E + A
A+B*C-D/E + AB
A+B*C-D/E +* AB
A+B*C-D/E +* ABC
A+B*C-D/E - A B C *+
A+B*C-D/E - A B C *+ D
A+B*C-D/E -/ A B C *+ D
A+B*C-D/E -/ A B C *+ D E
A+B*C-D/E A B C *+ D E/-
Infix to Postfix Example:
A * B - ( C + D ) + E
Infix Stack Postfix

a) A * B - ( C + D ) + E empty empty
b) * B - ( C + D ) + E empty A
c) B - ( C + D ) + E * A
d) - ( C + D ) + E * A B
e) - ( C + D ) + E empty A B *
f) ( C + D ) + E - A B *
g) C + D ) + E - ( A B *
h) + D ) + E - ( A B * C
i) D ) + E - ( + A B * C
j) ) + E - ( + A B * C D
k) + E - A B * C D +
l) + E empty A B * C D + -
m) E + A B * C D + -
n) + A B * C D + - E
o) empty A B * C D + - E +
Postfix Evaulation
 Read the input symbol from left to right
 Operand: push
 Operator: pop 2 operands, do the math, pop result
back onto stack

1 2 3 + *

Postfix Stack( bot -> top )


a) 1 2 3 + *
b) 2 3 + * 1
c) 3 + * 1 2
d) + * 1 2 3
e) * 1 5 // 5 from 2 + 3
f) 5 // 5 from 1 * 5
Backtracking
 Stacks can be used to backtrack to achieve certain
goals.
 Usually, we set up backtrack tokens to indicate a
backtrack opportunity.
Prefix Expression

 Polish Notation
 Introduced in 1924
 Operators are placed before operand
 Eg: + a b
Infix to Prefix Conversion
1. Scan the expression from right to left for each
character.
2. If the input is an operand then place it in the output
string
3. If the input is operator then push it into the stack.
❖ If the input operator is having highest or equal priority
than stack top operator then push it into the stack.

❖ If the input operator is having lowest priority then pop


the stack operator and place it in the output string and
push the input character to the stack.
Infix to Prefix Conversion
4. If the input is ‘(‘ parenthesis then push all the
operators of input into the stack until ‘)’ parenthesis
occurs.
5. If the input is ‘)’ then pop all the operators until
‘(‘and place it in the output string and discard the
parenthesis.
6. If the expression is completely scanned then pop
all the operators from stack and add it to output
string.
7. Reverse the Output string and display the result
Infix to Prefix Example:
2 * 3 + 5 * 4 - 9
Infix Stack Prefix

9 empty 9
- - 9
4 - 9 4
* - * 9 4
5 - * 9 4 5
+ - + 9 4 5 *
3 - + 9 4 5 * 3
* - + * 9 4 5 * 3
2 - + * 9 4 5 * 3 2
empty 9 4 5 * 3 2 * + -

Output : - + * 2 3 * 5 4 9
Prefix Evaluation
 Scan the expression from Right to left
 If operand – push into the stack

 If operator – pop two operators and evaluate


and push the result back to the stack.

 Evaluate the following expression


-+*23*549

Output : 17
Quick test
 Stacks are _____________data structures.
 A stack is a particular kind of abstract data type or
collection in which the only operations on the collection
are the addition of an entity to the collection, known as
_____ and removal of an entity, known as __________
 All deletions and insertions occur at one end of the
stack known as the ________.
 Data going into the stack first, leaves out last.
 Stacks are also known as _____ data structures
Quick test
1. Process of inserting an element in stack is called
____________
a) Create ANS: B
b) Push
c) Evaluation
d) Pop
2. Process of removing an element from stack is called
_____
a) Create
b) Push ANS: D
c) Evaluation
d) Pop
Quick test
3. In a stack, if a user tries to remove an element from
an empty stack it is called _________
a) Underflow ANS: A
b) Empty collection
c) Overflow
d) Garbage Collection
4. Pushing an element into stack already having five elements
and stack size of 5, then stack becomes ___________
a) Overflow
b) Crash
ANS: A
c) Underflow
d) User flow
Quick test
5.Which element would get first deleted from the stack?
a) The element in the middle of the stack ANS: B
b) The element at the top of the stack
c) The element at the bottom of the stack

6. A stack is a ____ data structure that follows the ___principle

ANS: Linear , LIFO


Important questions
 Stacks
 Definition, working principle , ADT
 Algorithm

 Program – Stack Operations


 Time complexity & Applications

 Stack Applications
 INFIX to prefix, postfix – problem, algorithm, code

 PREFIX , POSTFIX expression evaluations – problem,


algorithm, code
Important questions
 QUEUE
 Definition, working principle , ADT
 Algorithm

 Program – Queue Operations


 Time complexity & Real life Applications

 Circular Queue - algorithm, code


 Priority Queue - algorithm, code
 Difference, advantages and disadvantages
between Linear queue and Circular Queue
References
 Data Structures: A Pseudocode Apporoach with C. Gilberg, Richard F.,
Forouzan, Behrouz A.. PWS Publishing Company: 1998

You might also like