SlideShare a Scribd company logo
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

PPT
Data Structure and Algorithms Queues
PDF
PPTX
Queue - Data Structure - Notes
PPTX
Stack - Data Structure - Notes
PPTX
Stack - Data Structure
PPTX
Queue Implementation Using Array & Linked List
PPT
Stack Data Structure V1.0
PDF
STACK ( LIFO STRUCTURE) - Data Structure
Data Structure and Algorithms Queues
Queue - Data Structure - Notes
Stack - Data Structure - Notes
Stack - Data Structure
Queue Implementation Using Array & Linked List
Stack Data Structure V1.0
STACK ( LIFO STRUCTURE) - Data Structure

What's hot (20)

PPTX
My lectures circular queue
PPT
Stacks and queues
PPT
Data structure lecture7
PPTX
Stacks in Data Structure
PPSX
Data Structure (Queue)
PPT
QUEUE IN DATA STRUCTURE USING C
PPT
Stacks queues
PPTX
Stack and queue
PPT
Algorithm: priority queue
PPTX
STACKS IN DATASTRUCTURE
PPTX
Stacks in DATA STRUCTURE
PPTX
Queue in Data Structure
PPTX
2.3 graphs of functions
PPT
03 stacks and_queues_using_arrays
PDF
getdata.pdf
PPTX
Queues presentation
PPTX
Queues in C++
PPTX
Application of Stack - Yadraj Meena
My lectures circular queue
Stacks and queues
Data structure lecture7
Stacks in Data Structure
Data Structure (Queue)
QUEUE IN DATA STRUCTURE USING C
Stacks queues
Stack and queue
Algorithm: priority queue
STACKS IN DATASTRUCTURE
Stacks in DATA STRUCTURE
Queue in Data Structure
2.3 graphs of functions
03 stacks and_queues_using_arrays
getdata.pdf
Queues presentation
Queues in C++
Application of Stack - Yadraj Meena
Ad

Similar to Data Structure and Algorithms Stacks (20)

PPTX
Lecture 6 data structures and algorithms
PPT
PPTX
5.stack
PPTX
Stacks and queues using aaray line .pptx
PPTX
PPTX
Stacks Data structure.pptx
PPT
Stack in Data Structure
PPTX
Data strutcure and annalysis topic stack
PPTX
Stacks IN DATA STRUCTURES
PPT
Stacks
PPTX
Stack Data Structure Intro and Explanation
PPTX
DS-UNIT 3 FINAL.pptx
PPTX
Data Structures: Stacks (Part 1)
PPT
Unit 3 stack
PPTX
Stacks
PPT
Stack Operation In Data Structure
PPTX
Chapter 5-stack.pptx
PDF
Data structures stacks
PPTX
Stack & Queue in Data Structure and Algorithms
PPTX
Unit 3 Stacks and Queues.pptx
Lecture 6 data structures and algorithms
5.stack
Stacks and queues using aaray line .pptx
Stacks Data structure.pptx
Stack in Data Structure
Data strutcure and annalysis topic stack
Stacks IN DATA STRUCTURES
Stacks
Stack Data Structure Intro and Explanation
DS-UNIT 3 FINAL.pptx
Data Structures: Stacks (Part 1)
Unit 3 stack
Stacks
Stack Operation In Data Structure
Chapter 5-stack.pptx
Data structures stacks
Stack & Queue in Data Structure and Algorithms
Unit 3 Stacks and Queues.pptx
Ad

More from ManishPrajapati78 (14)

PPT
Data Structure and Algorithms Binary Search Tree
PPT
Data Structure and Algorithms Binary Tree
PPTX
Data Structure and Algorithms Merge Sort
PPTX
Data Structure and Algorithms The Tower of Hanoi
PPT
Data Structure and Algorithms Linked List
PPT
Data Structure and Algorithms Sorting
PPT
Data Structure and Algorithms Arrays
PPT
Data Structure and Algorithms
PPT
Data Structure and Algorithms Hashing
PPTX
Data Structure and Algorithms Graph Traversal
PPT
Data Structure and Algorithms Graphs
PPT
Data Structure and Algorithms Huffman Coding Algorithm
PPT
Data Structure and Algorithms Heaps and Trees
PPT
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms Linked List
Data Structure and Algorithms Sorting
Data Structure and Algorithms Arrays
Data Structure and Algorithms
Data Structure and Algorithms Hashing
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graphs
Data Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms AVL Trees

Recently uploaded (20)

PDF
Build Multi-agent using Agent Development Kit
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Become an Agentblazer Champion Challenge Kickoff
PDF
How to Confidently Manage Project Budgets
PPTX
Presentation of Computer CLASS 2 .pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
AI in Product Development-omnex systems
PPT
Introduction Database Management System for Course Database
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
PPTX
AIRLINE PRICE API | FLIGHT API COST |
PDF
A REACT POMODORO TIMER WEB APPLICATION.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
PPTX
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
PDF
System and Network Administraation Chapter 3
Build Multi-agent using Agent Development Kit
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Become an Agentblazer Champion Challenge Kickoff
How to Confidently Manage Project Budgets
Presentation of Computer CLASS 2 .pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
How to Migrate SBCGlobal Email to Yahoo Easily
AI in Product Development-omnex systems
Introduction Database Management System for Course Database
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Upgrade and Innovation Strategies for SAP ERP Customers
Odoo POS Development Services by CandidRoot Solutions
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
The Role of Automation and AI in EHS Management for Data Centers.pdf
AIRLINE PRICE API | FLIGHT API COST |
A REACT POMODORO TIMER WEB APPLICATION.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
System and Network Administraation Chapter 3

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))