SlideShare a Scribd company logo
1 of 26
īƒ˜Introduction: List and Array representations,
īƒ˜Operations on stack (traversal, push and pop)
īƒ˜Arithmetic expressions: polish notation, evaluation
and transformation of expressions.
2
Introduction to Stacks
ī‚—Consider a card game with a discard pile
ī‚—Discards always placed on the top of the pile
ī‚—Players may retrieve a card only from the top
ī‚—We seek a way to represent and manipulate this
in a computer program
ī‚—This is a stack
What other examples
can you think of that
are modeled by a stack?
What other examples
can you think of that
are modeled by a stack?
3
Introduction to Stacks
ī‚—A stack is a last-in-first-out (LIFO) data structure
ī‚—Adding an item
ī‚—Referred to as pushing it onto the stack
ī‚—Removing an item
ī‚—Referred to as
popping it from
the stack
4
Stack
ī‚—Definition:
ī‚—An ordered collection of data items
ī‚—Can be accessed at only one end (the top)
ī‚—Operations:
ī‚—construct a stack (usually empty)
ī‚—check if it is empty
ī‚—Push: add an element to the top
ī‚—Top: retrieve the top element
ī‚—Pop: remove the top element
Stack
items[MAX-1]
. .
. .
. .
items[2] C
items[1] B
items[0] A
Top=2
Insert an item A
ī‚—A new item (A) is inserted at the Top of the stack
items[MAX-1]
. .
. .
items[3]
items[2]
items[1]
items[0] A Top=0
Insert an item B
ī‚—A new item (B) is inserted at the Top of the stack
items[MAX-1]
. .
. .
items[3]
items[2]
items[1] B Top=1
items[0] A
Insert an item C
ī‚—A new item (C) is inserted at the Top of the stack
items[MAX-1]
. .
. .
items[3]
items[2] C Top=2
items[1] B
items[0] A
Insert an item D
ī‚—A new item (D) is inserted at the Top of the stack
items[MAX-1]
. .
. .
items[3] D Top=3
items[2] C
items[1] B
items[0] A
Insert Operation(Array)
PUSH(STACK, N, TOP, ITEM)
1. If TOP=N:
write OVERFLOW, and Return.
2. Set TOP := TOP + 1.
3. Set STACK[TOP]:= ITEM.
4. Return.
Delete D
ī‚—an item (D) is deleted from the Top of the stack
items[MAX-1]
. .
. .
items[3] D
items[2] C Top=2
items[1] B
items[0] A
Delete C
ī‚—an item (C) is deleted from the Top of the stack.
items[MAX-1]
. .
. .
items[3] D
items[2] C
items[1] B Top=1
items[0] A
Delete B
ī‚—an item (B) is deleted from the Top of the stack
items[MAX-1]
. .
. .
items[3] D
items[2] C
items[1] B
items[0] A Top=0
Delete A
ī‚—an item (A) is deleted from the Top of the stack.
items[MAX-1]
. .
items[4]
items[3] D
items[2] C
items[1] B
items[0] A Top=-1
Delete Operation(Array)
POP(STACK, N, TOP, ITEM)
1. If TOP = NULL then :
write: UNDERFLOW, and Return.
2. Set ITEM := STACK [TOP].
3. Set TOP := TOP - 1.
4. Return.
Insert Operation(LL)
PUSH(INFO, LINK, TOP, AVAIL, ITEM)
1. If AVAIL = NULL, then :
write OVERFLOW, and Exit.
2. Set NEW := AVAIL and AVAIL := LINK[AVAIL]
3. Set INFO[NEW]:= ITEM
4. Set LINK[NEW] := TOP
5. Set TOP := NEW
6.Exit
Delete Operation(LL)
POP(INFO, LINK, TOP, AVAIL, ITEM)
1. If TOP = NULL, then :
write UNDERFLOW, and Exit.
2. Set ITEM :=INFO[TEMP]
3. Set TEMP := TOP and TOP :=LINK[TOP]
4. LINK[TEMP] = AVAIL and AVAIL = TEMP
5. Exit
18
Postfix Notation(RPN)
ī‚—Polish notation, also known as prefix notation.
ī‚—It is a symbolic logic invented by Polish mathematician
Jan Lukasiewicz in the 1920's.
ī‚—Most compilers convert an expression in infix
notation to postfix
ī‚—the operators are written after the operands
ī‚—So a * b + c becomes a b * c +
ī‚—Advantage:
ī‚—expressions can be written without parentheses
19
Postfix and Prefix Examples
INFIX POSTFIX PREFIX
A + B
A * B + C
A * (B + C)
A - (B - (C - D))
A - B - C - D
A B * C +
A B C + *
A B C D---
A B-C-D-
+ * A B C
* A + B C
-A-B-C D
---A B C D
A B + + A B
Prefix : Operators come
before the operands
Prefix : Operators come
before the operands
20
→ 2 7 5 6 - - *
→ 2 7 5 6 - - *
→ 2 7 -1 - *
→ 2 7 -1 - * →
"By hand" (Underlining technique):
1. Scan the expression from left to right to find an operator.
2. Locate ("underline") the last two preceding operands
and combine them using this operator.
3. Repeat until the end of the expression is reached.
Example:
2 3 4 + 5 6 - - *
→ 2 3 4 + 5 6 - - *
2 8 * → 2 8 * → 16
Evaluating RPN Expressions
īƒ˜ P is an arithmetic expression in Postfix Notation.
1. Add a right parenthesis “)” at the end of P.
2. Scan P from left to right and Repeat Step 3 and 4 for each element of P
until the sentinel “)” is encountered.
3. If an operand is encountered, put it on STACK.
4. If an operator @ is encountered, then:
(a) Remove the two top elements of STACK, where A
is the top element and B is the next to top element.
(b) Evaluate B @ A.
(c) Place the result of (b) back on STACK.
[End of if structure.]
[End of step 2 Loop.]
5. Set VALUE equal to the top element on STACK.
6. Exit.
21
22
Evaluating
RPN
Expressions
ī‚—Note the
changing
status of the
stack
23
By hand: "Fully parenthesize-move-erase" method:
1. Fully parenthesize the expression.
2. Replace each right parenthesis by the corresponding
operator.
3. Erase all left parentheses.
Examples:
A * B + C →
→ ((A B * C +
→ A B * C +
A * (B + C) →
→ (A (B C + *
→ A B C + *
((A * B) + C) (A * (B + C) )
24
Stack Algorithm
POLISH (Q, P)
1. PUSH “(” on to STACK and add “)” to the end of Q.
2. Scan Q from left to right and Repeat steps 3 to 6 for each element of Q until
the STACK is empty:
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator is encountered, then:
(a) Repeatedly POP from STACK and add to P each
operator (On the TOP of STACK) which has the same precedence as or
higher precedence than @.
(b) Add @ to STACK.
[End of If structure.]
6. If a right parenthesis is encountered, then:
(a) Repeatedly POP from STACK and add to P each operator (On the
TOP of STACK.) until a left parenthesis is encountered.
(b) Remove the left parenthesis. [Don’t add the left parenthesis to P.]
[End of If Structure.]
[End of step 2 Loop.]
7. Exit.
25
By hand: "Fully parenthesize-move-erase" method:
1. Fully parenthesize the expression.
2. Replace each left parenthesis by the corresponding
operator.
3. Erase all right parentheses.
Examples:
A * B + C →
→ + * A B) C)
→ + * A B C
A * (B + C) →
→ * A + B C ))
→ * A + B C
((A * B) + C) (A * (B + C))
Thank You

More Related Content

What's hot

My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queueSenthil Kumar
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queuesTrupti Agrawal
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
QUEUE IN DATA STRUCTURE USING C
QUEUE IN DATA STRUCTURE USING CQUEUE IN DATA STRUCTURE USING C
QUEUE IN DATA STRUCTURE USING CMeghaj Mallick
 
Stacks queues
Stacks queuesStacks queues
Stacks queuesRajendran
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queueTareq Hasan
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTUREArchie Jamwal
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTUREMandeep Singh
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 
2.3 graphs of functions
2.3 graphs of functions2.3 graphs of functions
2.3 graphs of functionshisema01
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arraystameemyousaf
 
getdata.pdf
getdata.pdfgetdata.pdf
getdata.pdfKAMALKANDI1
 
Queues presentation
Queues presentationQueues presentation
Queues presentationToseef Hasan
 
Queues in C++
Queues in C++Queues in C++
Queues in C++Vineeta Garg
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaDipayan Sarkar
 

What's hot (20)

My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Stacks in Data Structure
Stacks in Data StructureStacks in Data Structure
Stacks in Data Structure
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
QUEUE IN DATA STRUCTURE USING C
QUEUE IN DATA STRUCTURE USING CQUEUE IN DATA STRUCTURE USING C
QUEUE IN DATA STRUCTURE USING C
 
Stacks queues
Stacks queuesStacks queues
Stacks queues
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Queue
QueueQueue
Queue
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
2.3 graphs of functions
2.3 graphs of functions2.3 graphs of functions
2.3 graphs of functions
 
Queue
QueueQueue
Queue
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
getdata.pdf
getdata.pdfgetdata.pdf
getdata.pdf
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 

Similar to Data Structure and Algorithms Stacks

Unit 3 stack
Unit 3   stackUnit 3   stack
Unit 3 stackkalyanineve
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-listpinakspatel
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxhaaamin01
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacksmaamir farooq
 
Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsAakash deep Singhal
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.pptOliverKane3
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.pptOliverKane3
 
Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure DivyeshKumar Jagatiya
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)mailmerk
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And RecursionAshim Lamichhane
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptxMouDhara1
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Stack application
Stack applicationStack application
Stack applicationStudent
 
13 Stacks and Queues.pptx
13 Stacks and Queues.pptx13 Stacks and Queues.pptx
13 Stacks and Queues.pptxssuserb7c8b8
 
stacks and queues
stacks and queuesstacks and queues
stacks and queuesDurgaDeviCbit
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 

Similar to Data Structure and Algorithms Stacks (20)

Unit 3 stack
Unit 3   stackUnit 3   stack
Unit 3 stack
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 
Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithms
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Stack push pop
Stack push popStack push pop
Stack push pop
 
Stacks
StacksStacks
Stacks
 
Stack application
Stack applicationStack application
Stack application
 
13 Stacks and Queues.pptx
13 Stacks and Queues.pptx13 Stacks and Queues.pptx
13 Stacks and Queues.pptx
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Data structures
Data structures Data structures
Data structures
 

More from ManishPrajapati78

Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeManishPrajapati78
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeManishPrajapati78
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortManishPrajapati78
 
Data Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of HanoiData Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of HanoiManishPrajapati78
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
Data Structure and Algorithms Sorting
Data Structure and Algorithms SortingData Structure and Algorithms Sorting
Data Structure and Algorithms SortingManishPrajapati78
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms ArraysManishPrajapati78
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms ManishPrajapati78
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms HashingManishPrajapati78
 
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph TraversalData Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph TraversalManishPrajapati78
 
Data Structure and Algorithms Graphs
Data Structure and Algorithms GraphsData Structure and Algorithms Graphs
Data Structure and Algorithms GraphsManishPrajapati78
 
Data Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding AlgorithmData Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding AlgorithmManishPrajapati78
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesManishPrajapati78
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesManishPrajapati78
 

More from ManishPrajapati78 (14)

Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
 
Data Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of HanoiData Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of Hanoi
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Data Structure and Algorithms Sorting
Data Structure and Algorithms SortingData Structure and Algorithms Sorting
Data Structure and Algorithms Sorting
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
 
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph TraversalData Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph Traversal
 
Data Structure and Algorithms Graphs
Data Structure and Algorithms GraphsData Structure and Algorithms Graphs
Data Structure and Algorithms Graphs
 
Data Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding AlgorithmData Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding Algorithm
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and Trees
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
 

Recently uploaded

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 

Recently uploaded (20)

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi đŸĢĻ HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi đŸĢĻ HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi đŸĢĻ HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi đŸĢĻ HOT AND SEXY VVIP 🍎 SE...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 

Data Structure and Algorithms Stacks

  • 1. īƒ˜Introduction: List and Array representations, īƒ˜Operations on stack (traversal, push and pop) īƒ˜Arithmetic expressions: polish notation, evaluation and transformation of expressions.
  • 2. 2 Introduction to Stacks ī‚—Consider a card game with a discard pile ī‚—Discards always placed on the top of the pile ī‚—Players may retrieve a card only from the top ī‚—We seek a way to represent and manipulate this in a computer program ī‚—This is a stack What other examples can you think of that are modeled by a stack? What other examples can you think of that are modeled by a stack?
  • 3. 3 Introduction to Stacks ī‚—A stack is a last-in-first-out (LIFO) data structure ī‚—Adding an item ī‚—Referred to as pushing it onto the stack ī‚—Removing an item ī‚—Referred to as popping it from the stack
  • 4. 4 Stack ī‚—Definition: ī‚—An ordered collection of data items ī‚—Can be accessed at only one end (the top) ī‚—Operations: ī‚—construct a stack (usually empty) ī‚—check if it is empty ī‚—Push: add an element to the top ī‚—Top: retrieve the top element ī‚—Pop: remove the top element
  • 5. Stack items[MAX-1] . . . . . . items[2] C items[1] B items[0] A Top=2
  • 6. Insert an item A ī‚—A new item (A) is inserted at the Top of the stack items[MAX-1] . . . . items[3] items[2] items[1] items[0] A Top=0
  • 7. Insert an item B ī‚—A new item (B) is inserted at the Top of the stack items[MAX-1] . . . . items[3] items[2] items[1] B Top=1 items[0] A
  • 8. Insert an item C ī‚—A new item (C) is inserted at the Top of the stack items[MAX-1] . . . . items[3] items[2] C Top=2 items[1] B items[0] A
  • 9. Insert an item D ī‚—A new item (D) is inserted at the Top of the stack items[MAX-1] . . . . items[3] D Top=3 items[2] C items[1] B items[0] A
  • 10. Insert Operation(Array) PUSH(STACK, N, TOP, ITEM) 1. If TOP=N: write OVERFLOW, and Return. 2. Set TOP := TOP + 1. 3. Set STACK[TOP]:= ITEM. 4. Return.
  • 11. Delete D ī‚—an item (D) is deleted from the Top of the stack items[MAX-1] . . . . items[3] D items[2] C Top=2 items[1] B items[0] A
  • 12. Delete C ī‚—an item (C) is deleted from the Top of the stack. items[MAX-1] . . . . items[3] D items[2] C items[1] B Top=1 items[0] A
  • 13. Delete B ī‚—an item (B) is deleted from the Top of the stack items[MAX-1] . . . . items[3] D items[2] C items[1] B items[0] A Top=0
  • 14. Delete A ī‚—an item (A) is deleted from the Top of the stack. items[MAX-1] . . items[4] items[3] D items[2] C items[1] B items[0] A Top=-1
  • 15. Delete Operation(Array) POP(STACK, N, TOP, ITEM) 1. If TOP = NULL then : write: UNDERFLOW, and Return. 2. Set ITEM := STACK [TOP]. 3. Set TOP := TOP - 1. 4. Return.
  • 16. Insert Operation(LL) PUSH(INFO, LINK, TOP, AVAIL, ITEM) 1. If AVAIL = NULL, then : write OVERFLOW, and Exit. 2. Set NEW := AVAIL and AVAIL := LINK[AVAIL] 3. Set INFO[NEW]:= ITEM 4. Set LINK[NEW] := TOP 5. Set TOP := NEW 6.Exit
  • 17. Delete Operation(LL) POP(INFO, LINK, TOP, AVAIL, ITEM) 1. If TOP = NULL, then : write UNDERFLOW, and Exit. 2. Set ITEM :=INFO[TEMP] 3. Set TEMP := TOP and TOP :=LINK[TOP] 4. LINK[TEMP] = AVAIL and AVAIL = TEMP 5. Exit
  • 18. 18 Postfix Notation(RPN) ī‚—Polish notation, also known as prefix notation. ī‚—It is a symbolic logic invented by Polish mathematician Jan Lukasiewicz in the 1920's. ī‚—Most compilers convert an expression in infix notation to postfix ī‚—the operators are written after the operands ī‚—So a * b + c becomes a b * c + ī‚—Advantage: ī‚—expressions can be written without parentheses
  • 19. 19 Postfix and Prefix Examples INFIX POSTFIX PREFIX A + B A * B + C A * (B + C) A - (B - (C - D)) A - B - C - D A B * C + A B C + * A B C D--- A B-C-D- + * A B C * A + B C -A-B-C D ---A B C D A B + + A B Prefix : Operators come before the operands Prefix : Operators come before the operands
  • 20. 20 → 2 7 5 6 - - * → 2 7 5 6 - - * → 2 7 -1 - * → 2 7 -1 - * → "By hand" (Underlining technique): 1. Scan the expression from left to right to find an operator. 2. Locate ("underline") the last two preceding operands and combine them using this operator. 3. Repeat until the end of the expression is reached. Example: 2 3 4 + 5 6 - - * → 2 3 4 + 5 6 - - * 2 8 * → 2 8 * → 16
  • 21. Evaluating RPN Expressions īƒ˜ P is an arithmetic expression in Postfix Notation. 1. Add a right parenthesis “)” at the end of P. 2. Scan P from left to right and Repeat Step 3 and 4 for each element of P until the sentinel “)” is encountered. 3. If an operand is encountered, put it on STACK. 4. If an operator @ is encountered, then: (a) Remove the two top elements of STACK, where A is the top element and B is the next to top element. (b) Evaluate B @ A. (c) Place the result of (b) back on STACK. [End of if structure.] [End of step 2 Loop.] 5. Set VALUE equal to the top element on STACK. 6. Exit. 21
  • 23. 23 By hand: "Fully parenthesize-move-erase" method: 1. Fully parenthesize the expression. 2. Replace each right parenthesis by the corresponding operator. 3. Erase all left parentheses. Examples: A * B + C → → ((A B * C + → A B * C + A * (B + C) → → (A (B C + * → A B C + * ((A * B) + C) (A * (B + C) )
  • 24. 24 Stack Algorithm POLISH (Q, P) 1. PUSH “(” on to STACK and add “)” to the end of Q. 2. Scan Q from left to right and Repeat steps 3 to 6 for each element of Q until the STACK is empty: 3. If an operand is encountered, add it to P. 4. If a left parenthesis is encountered, push it onto STACK. 5. If an operator is encountered, then: (a) Repeatedly POP from STACK and add to P each operator (On the TOP of STACK) which has the same precedence as or higher precedence than @. (b) Add @ to STACK. [End of If structure.] 6. If a right parenthesis is encountered, then: (a) Repeatedly POP from STACK and add to P each operator (On the TOP of STACK.) until a left parenthesis is encountered. (b) Remove the left parenthesis. [Don’t add the left parenthesis to P.] [End of If Structure.] [End of step 2 Loop.] 7. Exit.
  • 25. 25 By hand: "Fully parenthesize-move-erase" method: 1. Fully parenthesize the expression. 2. Replace each left parenthesis by the corresponding operator. 3. Erase all right parentheses. Examples: A * B + C → → + * A B) C) → + * A B C A * (B + C) → → * A + B C )) → * A + B C ((A * B) + C) (A * (B + C))