0% found this document useful (0 votes)
19 views13 pages

Lecture 03 - Stacks

Uploaded by

ahababimtiaz
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)
19 views13 pages

Lecture 03 - Stacks

Uploaded by

ahababimtiaz
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/ 13

ِ‫ﺑِﺳْ مِ ٱ ﱠٰ ِ ٱﻟرﱠ ﺣْ َٰﻣ ِن ٱﻟرﱠ ﺣِﯾم‬

In the name of Allah, Most Gracious, Most Merciful

CSE 4303
Data Structure

Topic: Stacks & Its Applications

Asaduzzaman Herok
Lecturer | CSE | IUT
[email protected]
Stacks
According to the Merriam webster dictionary:
Stack(noun): a large usually conical pile (as of hay, straw, or grain in the sheaf) left standing
in the field for storage
Stack(verb): to arrange in a pile or to pile in or on.
Stack(Data structure): A data structure where elements are arranged in pile and supports
last-in–first-out (LIFO) behavior.

Asaduzzaman Herok, Lecturer


01 September, 2023 2
Department of Computer Science and Engineering, IUT
Why do need Stack
Applications of Stack:
➢ Parsing code:
○ Matching parenthesis
○ XML (e.g., XHTML)
➢ Tracking function calls
➢ Dealing with undo/redo operations
➢ Reversing a list
➢ Conversion of an infix expression into a postfix expression
➢ Evaluation of a postfix expression
➢ Conversion of an infix expression into a prefix expression
➢ Evaluation of a prefix expression
➢ Recursion
➢ ………

Asaduzzaman Herok, Lecturer


01 September, 2023 3
Department of Computer Science and Engineering, IUT
Stack ADT Interface
❏ boolean isEmpty(); // return true if empty
❏ boolean isFull(); // return true if full
❏ void push(item); // insert item into stack
❏ void pop(); // remove most recent item
❏ void clear(); // remove all items from stack
❏ Item top(); Item peek() // retrieve most recent item
❏ Item topAndPop(); // return & remove most recent item

Asaduzzaman Herok, Lecturer


01 September, 2023 4
Department of Computer Science and Engineering, IUT
Implementations
Our target is to make asymptotic run time of any stack operation is Θ(1).
We will look at two implementations of stacks:
❏ Singly linked lists
❏ One-ended arrays
Operations at the front of a singly linked list are For one-ended arrays, all operations at the back
all Θ(1) are Θ(1)

Asaduzzaman Herok, Lecturer


01 September, 2023 5
Department of Computer Science and Engineering, IUT
Implementations
❏ boolean isEmpty(); // return true if empty
❏ boolean isFull(); // return true if full
❏ void push(item); // insert item into stack
❏ void pop(); // remove most recent item
❏ void clear(); // remove all items from stack
❏ Item top(); Item peek() // retrieve most recent item
❏ Item topAndPop();
Follow the board Please

Asaduzzaman Herok, Lecturer


01 September, 2023 6
Department of Computer Science and Engineering, IUT
Application:
Reversing String

Asaduzzaman Herok, Lecturer


01 September, 2023 7
Department of Computer Science and Engineering, IUT
Application: Parsing (XHTML)
XHTML is made of nested
❏ opening tags, e.g., <some_identifier>, and
❏ matching closing tags, e.g., </some_identifier>

Nesting indicates that any closing tag must match the most recent opening tag
Strategy for parsing XHTML:
➔ read though the XHTML linearly
➔ place the opening tags in a stack
➔ when a closing tag is encountered, check that it matches what is on top of the stack

<html>
<head><title>Hello</title></head>
<body><p>This appears in the <i>browser</i>.</p></body>
</html>
Asaduzzaman Herok, Lecturer
01 September, 2023 8
Department of Computer Science and Engineering, IUT
Application:
Matching
Parenthesis

Asaduzzaman Herok, Lecturer


01 September, 2023 9
Department of Computer Science and Engineering, IUT
Infix, Postfix, Prefix Notation
Infix, postfix (Reverse-Polish), and prefix (Polish) notations are three different but equivalent
notations of writing algebraic expressions.

Infix Postfix Prefix


(a+b)*c ab+c* *+abc
a + ( b* c) abc*+ +a*bc

Infix form : <identifier> <operator> <identifier>


Postfix form : <identifier> <identifier> <operator>
Prefix form : <operator> <identifier> <identifier>

Asaduzzaman Herok, Lecturer


01 September, 2023 10
Department of Computer Science and Engineering, IUT
Application: Evaluate Postfix Notation
Infix notation: 9 – ((3 * 4) + 8) / 4
Postfix notation: 9 3 4 * 8 + 4 / –

Asaduzzaman Herok, Lecturer


01 September, 2023 11
Department of Computer Science and Engineering, IUT
Application: Expressions into Postfix Expressions
Infix: 𝑄 = 𝐴 + 𝐵 ∗ 𝐶 − 𝐷 / 𝐸 ↑ 𝐹 ∗ 𝐺 ∗ 𝐻
Postfix: P = ABC * + DEF ↑ / G * H * -

Asaduzzaman Herok, Lecturer


01 September, 2023 12
Department of Computer Science and Engineering, IUT
Acknowledgement

Rafsanjany Kushol
PhD Student, Dept. of Computing Science,
University of Alberta

Sabbir Ahmed
Assistant Professor
Department of CSE, IUT

Asaduzzaman Herok, Lecturer


01 September, 2023 13
Department of Computer Science and Engineering, IUT

You might also like