0% found this document useful (0 votes)
23 views37 pages

DSA # 3 Stack & Queues

Data Structure Queue

Uploaded by

AbD uL HAq kHAn
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)
23 views37 pages

DSA # 3 Stack & Queues

Data Structure Queue

Uploaded by

AbD uL HAq kHAn
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/ 37

Data Structure & Algorithms

Muzammil Khan

Department of
Computer & Software Technology
University of Swat

1
Chapter 3
Stack & Queue

Data Structure & Algorithms


2
Stack
 It is an ordered group of homogeneous items of elements.
 Elements are added to and removed from the top of the
stack
 The most recently added items are at the top of the stack
 The last element to be added is the first to be removed
 Therefore, called LIFO i.e. Last In and First Out
 For example, real world stack
 A deck of cards or
 A pile of books or
 A pile of plates, etc

Data Structure & Algorithms


3
Operation on a Stack
 A real-world stack allows operations at one end only
 For example,
 We can place or remove a card or plate from the top of the
stack only.
 Likewise,
 Stack allows all data operations at one end only
 At any given time, we can only access the top element of a
stack
 The element which is placed (inserted or added) last, is
accessed first
 The operations take place only at
 The TOP (pointer) of the stack
Data Structure & Algorithms
4
Operation on a Stack (Cont...)
 Basic operations
 These are two basic operations associated with stack
 Insertion operation is called PUSH operation and
 Removal operation is called POP operation

 Stack data structure is also called


 Piles and Push-down lists

 There are two ways to represent Stack in memory


1. Using array and
2. Using linked list

Data Structure & Algorithms


5
Array representation of STACK
 Usually the stacks are represented in the computer by a
linear array
 In the following algorithms/procedures of pushing and
popping an item from the stacks, considered
 A linear array STACK,
 A variable TOP which contain the location of the top
element of the stack; and
 A variable STACKSIZE which gives the maximum number
of elements that can be hold by the stack.

Data Structure & Algorithms


6
Example
 Consider the following stacks
 Dry run the algorithms for
 PUSH operation &
 POP operation

 STACKSIZE = 10
 ITEM = 55

Data Structure & Algorithms


7
Push Operation
 Overflow
 Adding element to a full stack array, lead to overflow error
 Algorithm (Plain Text)
 PUSH Operation
Step 1 - Checks if the stack is full.
Step 2 - If the stack is full, produces an error or message and
exit.
Step 3 - If the stack is not full, increments top to point next
empty space.
Step 4 - Adds data element to the stack location, where top is
pointing.
Step 5 - Returns success.

Data Structure & Algorithms


8
Push Operation (Cont...)
 Overflow
 Adding element to a full stack array, lead to overflow error
 Algorithm
 PUSH (STACK, TOP, STACKSIZE, ITEM)
Step 1. [Check if STACK is already filled?]
If TOP := STACKSIZE-1, then
Write: OVERFLOW / Stack Full, and Exit
Step 2. [Increase TOP by 1]
Set TOP := TOP+1
Step 3. [Insert ITEM in new TOP position]
Set STACK[TOP] := ITEM
Step 4. Exit

Data Structure & Algorithms


9
Pop Operation
 Underflow
 Removing an element from an empty stack array, lead to
underflow error

 Algorithm (Plain Text)


 PUSH Operation
Step 1 - Checks if the stack is empty.
Step 2 - If the stack is empty, produces an error and exit.
Step 3 - If the stack is not empty, accesses the data element at
which top is pointing.
Step 4 - Decreases the value of top by 1.
Step 5 - Returns success

Data Structure & Algorithms


10
Pop Operation (Cont...)
 Underflow
 Removing an element from an empty stack array, lead to
underflow error
 Algorithm
 PUSH (STACK, TOP, STACKSIZE, ITEM)
Step 1. [Check if STACK is already empty?]
If TOP := -1, then
Write: UNDERFLOW / Stack is Empty, and Exit
Step 2. [assign STACK[TOP] to ITEM]
Set ITEM := STACK[TOP]
Step 3. [Decrease TOP by 1]
Set TOP := TOP - 1
Step 4. Exit
Data Structure & Algorithms
11
Expression Parsing
 Expression parsing is one of the Stack application
 The way to write arithmetic expression is known as a
notation
 An arithmetic expression can be written in three different
but equivalent notations, i.e.,
 Without changing the essence or output of an expression.
1. Infix Notation
2. Prefix (Polish) Notation
3. Postfix (Reverse-Polish) Notation

 These notations are named as how they use operator in


expression
Data Structure & Algorithms
12
Expression Parsing (Cont...)
 Infix Notation
 Expression in infix notation
 For example, a – b + c
 Where operators are used in-between operands
 It is easy for humans 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 time and space consumption
 For example,
 (a + b) * c
 ((a + b) * c) – d

Data Structure & Algorithms


13
Expression Parsing (Cont...)
 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
 the operator is postfixed to the operands i.e.,
 The operator is written after the operands
 For example, ab+
 This is equivalent to its infix notation a+b
 Also known as Reversed Polish Notation
Data Structure & Algorithms
14
Examples
 Here are few equivalent examples

 It is not a very efficient way to design an algorithm or


program to parse infix notations
 Instead,
 These infix notations are first converted into either postfix
or prefix notations and Then computed
 Computers “prefer” postfix notation
Data Structure & Algorithms
15
Related Terms
 To parse any arithmetic expression, we need to take care of
operator precedence and associativity
 Operator Precedence
 When an operand is in between two different operators,
 Which operator will take the operand first, is
 Decided by the precedence of an operator
 For example, a + b * c  a + (b *c)
 Associativity
 Associativity describes the rule where operators with the
same precedence appear in an expression
 For example, in expression a+b-c, both + and – have the
same precedence, then

Data Structure & Algorithms


16
Related Terms
 Which part of the expression will be evaluated first
 Is determined by associativity of those operators
 Here, both + and - are left associative,
 So the expression will be evaluated as (a+b)-c

 Operator’s precedence and associativity

Data Structure & Algorithms


17
Infix to Postfix Conversation
 Algorithm that convert the infix expression IE into its
equivalent postfix expression PE
 It uses a stack to temporary hold the operators and left
parenthesis
 The postfix expression will be constructed from left to
right using operands from IE and operators popped from
STACK

 Algorithm’s Description
 Suppose IE is an arithmetic expression written in infix notation.
This algorithm finds the equivalent postfix expression PE

Data Structure & Algorithms


18
 Algorithm Statements
Step 1. Push “(“ onto STACK, and add “)” to the end of IE .
Step 2. Scan IE from left to right and repeat Steps 3 to 6 for each
element of IE until the STACK is empty
Step 3. If an operand is encountered, add it to PE.
Step 4. If a left parenthesis is encountered, push it onto STACK.
Step 5. If an operator © is encountered, then:
a) Repeatedly pop from STACK and add to PE each
operator (on the top of STACK) which has the same
or higher precedence/priority than ©
b) Add © to STACK.
Step 6. If a right parenthesis is encountered, then:
a) Repeatedly pop from STACK and add to PE each
operator (on the top of STACK) until a left
parenthesis is encountered.
b) Remove the left parenthesis.
Step 7. Exit [Do not add the left parenthesis to PE.]

19
Infix to Postfix Conversation (Cont...)
Example
 Let infix expression
 IE is A + ( B * C – ( D / E ^ F ) * G ) * H
 PE ?
 Step 1.
 Add “)” at the end of expression IE i.e.
 A + ( B * C – ( D / E ^ F ) * G ) * H ) and
 Also Push a “(“ on Stack
 Scan rest of the IE from left to right
 The table show
 Scan symbol
 Stack status
 PE (required expression)
Data Structure & Algorithms
20
21
Example (Cont...)
 Algorithm’s Description
 If PE is an arithmetic expression written in postfix notation. This
algorithm uses STACK to hold operands, and evaluate PE
 Algorithm’s Statements
Step 1. Add a Dollar Sign “$” at the end of PE
Step 2. Scan PE from left to right and repeat Steps 3 and 4 for each
element of PE until the sentinel “$” is encountered
Step 3. If an operand is encountered, put it on STACK
Step 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
Step 5. Set VALUE equal to the top element on STACK
Step 5. Exit

22
Computation of Postfix Expression
Example
 An infix arithmetic expression
 (5 + 2) * 3 – 9 / 3
 Its postfix expression is
 52+3*93/–
 Now add “$” at the end of expression as a sentinel
 52+3*84/–$

 Scan rest of the PE from left to right


 The table show
 Scan symbol
 Stack status
 Action or operation to do
Data Structure & Algorithms
23
Example (Cont...)

Data Structure & Algorithms


24
Homework
 Recursion
 Pages 176

Data Structure & Algorithms


25
Queue
 Queue is a data structure somewhat similar to Stacks
 Unlike stacks,
 A queue is open at both its ends
 Insertion can take place only at the other end, called
the “Rear”
 Rear is sometime referred as “Tail”
 Deletion can take place only at one end, called the “Front”
 Front is sometime referred as “Head”

 Queue follows FIFO i.e. First-In-First-Out methodology,


 The data item stored first will be accessed or processed first

Data Structure & Algorithms


26
Queue (Cont...)
 Real-world examples are
 One way road
 People line at bus stand

 There are main two ways to implement


1. Circular Array (array act as circular mode) Structures
2. Linked Structures
 Basic Operations on Queue
 Enqueue
 Insert an element at the rear of the queue
 Dequeue
 Remove an element from the front of the queue
Data Structure & Algorithms
27
Explanation (using Figure)
 Let the
 QUEUE is the queue
 MAXSIZE is the size of the queue
 FRONT is deletion pointer
 REAR is insertion pointer
 COUNT, is counter for number of data item in queue
 ITEM is data item to be inserted or deleted

 Let MAXSIZE = 6

Data Structure & Algorithms


28
Explanation (Cont...)

Data Structure & Algorithms


29
Explanation (Cont...)

Data Structure & Algorithms


30
Enqueue Operation
 Algorithm’s Statements
 ENQUEUE(QUEUE, MAXSIZE, FRONT, REAR,COUNT, ITEM)
Step 1. [QUEUE already filled?]
If COUNT = MAXSIZE then
Write: OVERFLOW, and Return
Step 2. [Find new value of REAR]
If COUNT= 0, then: [Queue initially empty]
Set FRONT= 0 and REAR = 0
Else: if REAR = MAXSIZE - 1, then:
Set REAR = 0
Else:
Set REAR = REAR+1
Step 3. Set QUEUE[REAR] = ITEM. [This insert new element]
Step 4. COUNT=COUNT+1 [Increment to Counter]
Step 5. Return
Data Structure & Algorithms
31
Dequeue Operation
 Algorithm’s Statements
 DEQUEUE(QUEUE, MAXSIZE, FRONT, REAR,COUNT, ITEM)
Step 1. [QUEUE already empty?]
If COUNT= 0, then: Write: UNDERFLOW, and Return
Step 2. Set ITEM = QUEUE[FRONT]
Step 3. Set COUNT = COUNT -1
Step 4. [Find new value of FRONT]
If COUNT = 0, then: [There was one element and deleted]
Set FRONT= -1, and REAR = -1 [set both to 0]
Else if FRONT= MAXSIZE, then: [Circular, so set Front = 0 ]
Set FRONT = 0
Else:
Set FRONT:=FRONT+1
Step 5. Return ITEM
Data Structure & Algorithms
32
Circular Queue Explanation

Data Structure & Algorithms


33
Circular Queue Explanation (Cont...)

Data Structure & Algorithms


34
Circular Queue Explanation (Cont...)

Data Structure & Algorithms


35
Homework
 Deque (Double Ended Queue)
 Pages 192

 Priority Queues
 Representation of priority queues
 List representation
 Array representation
 Pages 193-196

 Exercise
 Pages 196...

Data Structure & Algorithms


36
End of Chapter
 Assignment
 Implement “infix to postfix conversion” algorithm
 Where check for
 IE is A + ( B * C – ( D / E ^ F ) * G ) * H

 The implementation should be dynamic


 For all infix expression

 You may have quiz next week

Data Structure & Algorithms


37

You might also like