4.0 Stack
4.0 Stack
Algorithm (IT-209)
Instructor: Abdullah Javed
([email protected])
Lecturer,
Govt. Postgraduate College, Jhelum
Lecture 4.0
Fall 2020
Agenda
• Stack
Introduction
Operations
Methods of Implementation
Expression Parsing
2
Stack
•A stack is a linear data structure which can
be accessed only at one of its ends for
storing and retrieving data
• Sucha stack resembles a stack of trays in a
cafeteria: New trays are put on the top of the
stack and taken off the top
• Stack is a LIFO structure (Last In First Out)
• Atany time we can only access the top
element of a stack
3
Stack Operation
•A stack has following primary
operations:
Clear( ) – Clear the stack
isEmpty( ) – Check to see if stack is empty
isFull( ) – Check to see if stack is full
Push(el) – Put the element el on the top of the
stack
Pop( ) – Remove the topmost element from the
stack
Peek( ) – Return the top most element in the stack
without removing it
• At all times, we maintain a pointer to the last 4
Stack Operation (Diagram)
5
Applications
• Real life
Stack of trays in cafeteria
Piles of books in library
• Computer Science
Program Execution Stack
Evaluating Expressions and Syntax Parsing
Convert decimal to binary
Tower of Hanoi
Undo operations
6
Client, Interface, Implementation
• Separate interface and implementation.
Ex: stack, queue, bag, priority queue, symbol table, union-find, .…
• Benefits.
Client can't know details of implementation ⇒ client has many
implementation from which to choose.
Implementation can't know details of client needs ⇒ many clients can re-use
the same implementation.
Design: creates modular, reusable libraries.
Performance: use optimized implementation where it matters.
7
Implementation Methods
• Stack can be implemented by
Array
Maximum stack size is known ahead of time
Linked List
Maximum stack size unknown
8
Array based Stack
9
Array based Stack
10
Array based Stack
int stack[size]; Int Pop() {
} }
} return element;
bool isFull(){ }
} return top<0;
} 11
Linked List based Stack
• LIFO
– Push = Insert at head
– Pop = Remove at head
– IsEmpty = Empty List
– Clear = Delete List
• Pros
– Allows random and dynamic allocation of memory
– Constant time for push/pop operations
12
Applications
• Converting decimal to binary number
• Converting Infix notation to RPN/postfix
• Evaluating RPN
• Adding two very large numbers
• Backtracking
• Recognizing palindromes
13
Expression Parsing
• Theway to write arithmetic expression is
known as notation. An arithmetic expression
can be written in three different notations:
Infix Notation
Prefix (Polish) Notation
Postfix (Reverse-Polish) Notation
• Infix Notation
We write expression in infix notation, e.g. a + b – c,
where operators are used in-between operands. It
is easy for us to read, write and speak in infix
notation but the same does not go well with
computing devices. An algorithm to process infix
notation could be difficult and costly in terms of 14
Expression Parsing (Continue…)
• Prefix Notation
In this notation, operator is prefixed to operands,
i.e. operator is written ahead of operands. For
example, +ab. This is equivalent to its infix
notation a + b. Prefix notation is also known
as Polish Notation
• Postfix Notation
This notation style is known as Reversed Polish
Notation. In this notation style, the operator
is postfixed to the operands i.e., the operator is
written after the operands. For example, ab+. This 15
Expression Parsing (Continue…)
Sr. Infix Notation Prefix Postfix
No. Notation Notation
1 a+b +ab ab+
2 (a + b) ∗ c ∗+abc ab+c∗
3 a ∗ (b + c) ∗a+bc abc+∗
4 a/b+c/d +/ab/cd ab/cd/+
5 (a + b) ∗ (c + ∗+ab+cd ab+cd+∗
d)
6 ((a + b) ∗ c) - -∗+abcd ab+c∗d-
d to show the difference in all three notations
The table briefly tries
16
Infix to Postfix
17
Infix to Postfix using Stack
18
Expression Parsing (Continue…)
• Convert the following infix expression into
prefix and postfix
A + B * C + D
( A + B ) * ( C + D )
A + B + C + D
( A + B ) * C – ( D - E ) * ( F + G )
10 + 3 * 5 / ( 16 – 4 )
20
Example
Step wise evaluation of expression: 5, 6, 2, +, *, 12, 4, /, -
Symbol Scanned Stack
5 5
6 5, 6
2 5, 6, 2
+ 5, 8
* 40
12 40, 12
4 40, 12, 4
/ 40, 3
- 37 21
Evaluate RPN Expression
22
Evaluate RPN Expression
23
Convert Postfix to Infix Notation
24
Practice Exercises
• Convert the following infix notations to its equivalent postfix notations
• A * (B + (C+D)) * (E + F) / G) * H
• A+{(B+C)*(D+E)*F}/G
• A * ( B + D ) / E – F – (G + H / K)
• ( ( A * B ) * ( D / E ) ) / (F * G * H)
• Convert the following postfix notations to infix notations
• AB+C/D*
• ABC+/D*
• AB+CD++
• ABC++D+
25
Practice Exercises
• In practice exercise 2, assume A = 7.0, B = 4.0, C = 3.0 and D = 2.0.
Evaluate each postfix expression. Double check your answer.
• Evaluate the following postfix expressions
• 50 40 + 18 14 – 4 * +
• 100 40 8 + 20 10 – + *
• 5 6 9 + 80 5 * – /
• 120 45 20 + 25 15 – + *
26
Home Task
• Use the stack to reverse a string.
• Reverse the following:
27