SlideShare a Scribd company logo
3
Most read
6
Most read
8
Most read
More About Stacks:
Stack Applications
Dan Nguyen
CS 146, Spring 2004
Professor Sin-Min Lee
Quick Introduction
• Stacks are linear lists.
• 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.
Additional Notes
• Stacks structures are usually implemented
using arrays or linked lists.
• For both implementations, the running
time is O(n).
• We will be examining common Stack
Applications.
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
• Converting Decimal to Binary: Consider the following pseudocode
1) Read (number)
2) Loop (number > 0)
1) digit = number modulo 2
2) print (digit)
3) number = number / 2
// from Data Structures by Gilbert and Forouzan
The problem with this code is that it will print the binary
number backwards. (ex: 19 becomes 11001000 instead of 00010011. )
To remedy this problem, instead of printing the digit right away, we can
push it onto the stack. Then after the number is done being converted, we
pop the digit out of the stack and print it.
Stack Applications
• Postponement: 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
• Rules:
– Operands immediately go directly to output
– Operators are pushed into the stack (including parenthesis)
- Check to see if stack top operator is less than current operator
- If the top operator is less than, push the current operator onto stack
- If the top operator is greater than the current, pop top operator and
push onto stack, push current operator onto stack
- Priority 2: * /
- Priority 1: + -
- Priority 0: (
If we encounter a right parenthesis, pop from stack until we get
matching left parenthesis. Do not output parenthesis.
Infix to Postfix Example
A + B * C - D / E
Infix Stack(bot->top) Postfix
a) A + B * C - D / E
b) + B * C - D / E A
c) B * C - D / E + A
d) * C - D / E + A B
e) C - D / E + * A B
f) - D / E + * A B C
g) D / E + - A B C *
h) / E + - A B C * D
i) E + - / A B C * D
j) + - / A B C * D E
k) A B C * D E / - +
Infix to Postfix Example #2
A * B - ( C + D ) + E
Infix Stack(bot->top) 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
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.
References
• Our class textbook
• Data Structures: A Pseudocode Apporoach with C. Gilberg, Richard
F., Forouzan, Behrouz A.. PWS Publishing Company: 1998

More Related Content

What's hot (20)

PPSX
Stack
Seema Sharma
 
PPT
Linked lists
SARITHA REDDY
 
PPT
Linked List
CHANDAN KUMAR
 
PPT
Queue data structure
anooppjoseph
 
PPTX
Doubly Linked List
Ninad Mankar
 
PPTX
Abstract Data Types
karthikeyanC40
 
PPTX
Prefix, Infix and Post-fix Notations
Afaq Mansoor Khan
 
PPTX
Recursive Function
Harsh Pathak
 
PDF
Array data structure
maamir farooq
 
PPTX
stack & queue
manju rani
 
PDF
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
PPTX
Stack using Linked List
Sayantan Sur
 
PPTX
single linked list
Sathasivam Rangasamy
 
PPTX
Insertion in singly linked list
Keval Bhogayata
 
PPT
Queue implementation
Rajendran
 
PPTX
Presentation on queue
Rojan Pariyar
 
PPTX
Deque and its applications
Jsaddam Hussain
 
PPTX
arrays and pointers
Samiksha Pun
 
PPT
RECURSION IN C
v_jk
 
PPTX
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Linked lists
SARITHA REDDY
 
Linked List
CHANDAN KUMAR
 
Queue data structure
anooppjoseph
 
Doubly Linked List
Ninad Mankar
 
Abstract Data Types
karthikeyanC40
 
Prefix, Infix and Post-fix Notations
Afaq Mansoor Khan
 
Recursive Function
Harsh Pathak
 
Array data structure
maamir farooq
 
stack & queue
manju rani
 
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
Stack using Linked List
Sayantan Sur
 
single linked list
Sathasivam Rangasamy
 
Insertion in singly linked list
Keval Bhogayata
 
Queue implementation
Rajendran
 
Presentation on queue
Rojan Pariyar
 
Deque and its applications
Jsaddam Hussain
 
arrays and pointers
Samiksha Pun
 
RECURSION IN C
v_jk
 
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 

Viewers also liked (10)

DOC
Infix to-postfix examples
mua99
 
PPT
Stack Implementation
Zidny Nafan
 
PDF
DS Q&A
zzzubair
 
PPT
Application of Stacks
Ain-ul-Moiz Khawaja
 
PDF
Infix to Prefix (Conversion, Evaluation, Code)
Ahmed Khateeb
 
PPSX
Stacks Implementation and Examples
greatqadirgee4u
 
DOCX
Conversion from infix to prefix using stack
Haqnawaz Ch
 
PPT
Introduction to data structures and Algorithm
Dhaval Kaneria
 
PPTX
STACKS IN DATASTRUCTURE
Archie Jamwal
 
PPT
Lecture 1 data structures and algorithms
Aakash deep Singhal
 
Infix to-postfix examples
mua99
 
Stack Implementation
Zidny Nafan
 
DS Q&A
zzzubair
 
Application of Stacks
Ain-ul-Moiz Khawaja
 
Infix to Prefix (Conversion, Evaluation, Code)
Ahmed Khateeb
 
Stacks Implementation and Examples
greatqadirgee4u
 
Conversion from infix to prefix using stack
Haqnawaz Ch
 
Introduction to data structures and Algorithm
Dhaval Kaneria
 
STACKS IN DATASTRUCTURE
Archie Jamwal
 
Lecture 1 data structures and algorithms
Aakash deep Singhal
 
Ad

Similar to Stack application (20)

PPT
week 7,8,10,11 alll files included from .ppt
LidetAdmassu
 
PPTX
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
PDF
Data Structures And Algorithms(stacks queues)
lahariit406
 
PPTX
Implementation of stacks and queues in C
HarishKrishnanP
 
PPT
Stack Operation In Data Structure
DivyeshKumar Jagatiya
 
PDF
Data structure and algorithm.(dsa)
mailmerk
 
PPTX
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
devismileyrockz
 
PPTX
Stack and its Applications : Data Structures ADT
Soumen Santra
 
PPTX
Unit 3 Stacks and Queues.pptx
Yogesh Pawar
 
PPTX
DS MOD2 (1) (1).pptx
kumarkaushal17
 
PPTX
DSA_chapter_04_Stack and data structure and Queue.pptx
tahliildhoore54
 
PDF
Data structure lab manual
nikshaikh786
 
PPTX
PPT Lecture 3.2.1 stack newxxxxxxxxxx.pptx
AdarshPrajapati26
 
PPT
Stacks.ppt
OliverKane3
 
PPT
Stacks.ppt
OliverKane3
 
PPTX
introduction of the Stacks and Queues.pptx
kavitashingi123
 
PPT
Stack
Tejas Patel
 
PPTX
Stacks and queues using aaray line .pptx
ramkumar649780
 
PPT
12650891 (1).ppthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
RAtna29
 
week 7,8,10,11 alll files included from .ppt
LidetAdmassu
 
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Data Structures And Algorithms(stacks queues)
lahariit406
 
Implementation of stacks and queues in C
HarishKrishnanP
 
Stack Operation In Data Structure
DivyeshKumar Jagatiya
 
Data structure and algorithm.(dsa)
mailmerk
 
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
devismileyrockz
 
Stack and its Applications : Data Structures ADT
Soumen Santra
 
Unit 3 Stacks and Queues.pptx
Yogesh Pawar
 
DS MOD2 (1) (1).pptx
kumarkaushal17
 
DSA_chapter_04_Stack and data structure and Queue.pptx
tahliildhoore54
 
Data structure lab manual
nikshaikh786
 
PPT Lecture 3.2.1 stack newxxxxxxxxxx.pptx
AdarshPrajapati26
 
Stacks.ppt
OliverKane3
 
Stacks.ppt
OliverKane3
 
introduction of the Stacks and Queues.pptx
kavitashingi123
 
Stacks and queues using aaray line .pptx
ramkumar649780
 
12650891 (1).ppthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
RAtna29
 
Ad

More from Student (12)

PPTX
Cloud computing
Student
 
PPT
Keyword research
Student
 
PPTX
Agile Process models
Student
 
PPTX
Virtual technology
Student
 
PPTX
Student management system
Student
 
PPTX
Ip services
Student
 
PPT
Process models
Student
 
PPT
Student management system project report c++
Student
 
PPT
Application layer chapter-9
Student
 
PPT
Database recovery
Student
 
PPT
computer networks layers
Student
 
PPT
Uml struct2
Student
 
Cloud computing
Student
 
Keyword research
Student
 
Agile Process models
Student
 
Virtual technology
Student
 
Student management system
Student
 
Ip services
Student
 
Process models
Student
 
Student management system project report c++
Student
 
Application layer chapter-9
Student
 
Database recovery
Student
 
computer networks layers
Student
 
Uml struct2
Student
 

Recently uploaded (20)

PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
Wondershare Filmora Crack Free Download 2025
josanj305
 
PDF
Linux schedulers for fun and profit with SchedKit
Alessio Biancalana
 
PDF
Home Cleaning App Development Services.pdf
V3cube
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
Securing Model Context Protocol with Keycloak: AuthN/AuthZ for MCP Servers
Hitachi, Ltd. OSS Solution Center.
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
Survival Models: Proper Scoring Rule and Stochastic Optimization with Competi...
Paris Women in Machine Learning and Data Science
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PPTX
Role_of_Artificial_Intelligence_in_Livestock_Extension_Services.pptx
DrRajdeepMadavi
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Software Development Company Keene Systems, Inc (1).pdf
Custom Software Development Company | Keene Systems, Inc.
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pdf
ghjghvhjgc
 
PDF
NASA A Researcher’s Guide to International Space Station : Earth Observations
Dr. PANKAJ DHUSSA
 
PPTX
Manual Testing for Accessibility Enhancement
Julia Undeutsch
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Wondershare Filmora Crack Free Download 2025
josanj305
 
Linux schedulers for fun and profit with SchedKit
Alessio Biancalana
 
Home Cleaning App Development Services.pdf
V3cube
 
Digital Circuits, important subject in CS
contactparinay1
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Securing Model Context Protocol with Keycloak: AuthN/AuthZ for MCP Servers
Hitachi, Ltd. OSS Solution Center.
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Survival Models: Proper Scoring Rule and Stochastic Optimization with Competi...
Paris Women in Machine Learning and Data Science
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Role_of_Artificial_Intelligence_in_Livestock_Extension_Services.pptx
DrRajdeepMadavi
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Software Development Company Keene Systems, Inc (1).pdf
Custom Software Development Company | Keene Systems, Inc.
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pdf
ghjghvhjgc
 
NASA A Researcher’s Guide to International Space Station : Earth Observations
Dr. PANKAJ DHUSSA
 
Manual Testing for Accessibility Enhancement
Julia Undeutsch
 

Stack application

  • 1. More About Stacks: Stack Applications Dan Nguyen CS 146, Spring 2004 Professor Sin-Min Lee
  • 2. Quick Introduction • Stacks are linear lists. • 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).
  • 3. 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.
  • 4. Additional Notes • Stacks structures are usually implemented using arrays or linked lists. • For both implementations, the running time is O(n). • We will be examining common Stack Applications.
  • 5. 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)
  • 6. Stack Applications • Converting Decimal to Binary: Consider the following pseudocode 1) Read (number) 2) Loop (number > 0) 1) digit = number modulo 2 2) print (digit) 3) number = number / 2 // from Data Structures by Gilbert and Forouzan The problem with this code is that it will print the binary number backwards. (ex: 19 becomes 11001000 instead of 00010011. ) To remedy this problem, instead of printing the digit right away, we can push it onto the stack. Then after the number is done being converted, we pop the digit out of the stack and print it.
  • 7. Stack Applications • Postponement: 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.
  • 8. Infix to Postfix Conversion • Rules: – Operands immediately go directly to output – Operators are pushed into the stack (including parenthesis) - Check to see if stack top operator is less than current operator - If the top operator is less than, push the current operator onto stack - If the top operator is greater than the current, pop top operator and push onto stack, push current operator onto stack - Priority 2: * / - Priority 1: + - - Priority 0: ( If we encounter a right parenthesis, pop from stack until we get matching left parenthesis. Do not output parenthesis.
  • 9. Infix to Postfix Example A + B * C - D / E Infix Stack(bot->top) Postfix a) A + B * C - D / E b) + B * C - D / E A c) B * C - D / E + A d) * C - D / E + A B e) C - D / E + * A B f) - D / E + * A B C g) D / E + - A B C * h) / E + - A B C * D i) E + - / A B C * D j) + - / A B C * D E k) A B C * D E / - +
  • 10. Infix to Postfix Example #2 A * B - ( C + D ) + E Infix Stack(bot->top) 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 +
  • 11. Postfix Evaulation 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
  • 12. Backtracking • Stacks can be used to backtrack to achieve certain goals. • Usually, we set up backtrack tokens to indicate a backtrack opportunity.
  • 13. References • Our class textbook • Data Structures: A Pseudocode Apporoach with C. Gilberg, Richard F., Forouzan, Behrouz A.. PWS Publishing Company: 1998