0% found this document useful (0 votes)
24 views27 pages

4.0 Stack

Uploaded by

Sana Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views27 pages

4.0 Stack

Uploaded by

Sana Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Data Structure and

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

• When using an array to implement a stack


 The array's first element should represent the bottom of the
stack
 The last occupied location in the array represents the stack's
top

• This avoids shifting of elements of the array when


we push or remove elements from stack

9
Array based Stack

• Allocate an array of some size (pre-defined)


 Maximum N elements in stack

• Bottom stack element stored at element 0


• Last index in the array is the top
• Increment top when one element is pushed,
decrement after pop

10
Array based Stack
int stack[size]; Int Pop() {

int top = -1; int element = -1;

void Push(int element){ if(!isEmpty()){

if(!isFull()){ element = stack[top];

stack[++top] = element; stack[top--]=-1;

} }

} return element;

bool isFull(){ }

return top>size-1; bool isEmpty(){

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

• Whatis the result of the following postfix


expression
 17, 10, +, 3, *, 9, /
 5, 6, 2, +, *, 12, 4, /, -
19
Postfix Evaluation Algorithm
• Step 1 – Scan the expression from left to right
• Step 2 – If it is an operand push it to stack
• Step 3 – If it is an operator  then remove two top elements of stack,
where A is the top element and B is the next-to-top element and
compute B  A
• Step 4 – Store the output of Step 3, back to stack
• Step 5 – Scan the expression until all operands are consumed
• Step 6 – The top element on stack is the result
• Step 7 – End

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 – + *

Hint: Use both methods (stack and regular) for practice.

26
Home Task
• Use the stack to reverse a string.
• Reverse the following:

• Into the following string:

27

You might also like