Sei sulla pagina 1di 42

DATA STRUCTURES AND

ALGORITHMS

Lec # 8 Insructor: Engr. ARTA IFTIKHAR


Chapter 6: Stacks, Queues, Recursion

Last week :
 Doubly Linked List

 Introduction to Stacks

 Array representation of stacks

 PUSH, POP
 Minimizing Overflow
 Linked representation of Stacks
 PUSH, POP
This week:
 Arithmetic Expressions; Polish Notation

 Evaluation of Postfix Expression


 Transforming Infix into Postfix

 QuickSortk, An application of Stack


STACKS
 Introduction
 Real world Examples
 Example 6.1
 Postponed Decisions
 ARRAY representation of stacks
PUSH
Procedure 6.1:
PUSH(STACK,TOP,MAXSTK,ITEM)
1.If TOP=MAXSTK, then print OVERFLOW & return
2.Set TOP=TOP+1.
3.Set STACK[TOP]=ITEM
4.Return
POP
Procedure 6.2:
POP(STACK,TOP, ITEM)
1.If TOP=0, then print UNDERFLOW & return
2.Set ITEM=STACK[TOP]
3.Set TOP=TOP-1.
4.Return
Example
XXX YYY ZZZ
1 2 3 4 5 6 7 8

TOP = 3

MAXSTK= 8
Example 6.2
Simulate PUSH(STACK,WWW)
1.Since TOP=3, control goto step 2
2.TOP=3+1= 4
STCK[TOP]=STACK[4]=WWW
4.Return
(b) Simulate POP(STACK,ITEM)
1.Since TOP=3, control goto step 2
2.ITEM=ZZZ
3.TOP=3-1=2
4.Return
Minimizing overflow
Linked representation of Stacks
 Linked stack is implemented using singly linked list
 INFO fields of nodes hold elements of stack
 Link field contains neighboring element
 START act as TOP
 NULL pointer of last node act as bottom of stack
Procedure 6.3: Push link Stack
 PUSH_LINKSTACK(INFO, LINK, TOP,AVAIL,ITEM)
1.If avail=NULL, then write OVERFLOW & Exit
2. Set New=avail & avail=Link[avail]
3. Set Info[New]= Item
4. Set link[new]=Top
5. Set Top=New
6. Exit
Procedure 6.4: Pop linkStack
 POP_LINKSTACK(INFO, LINK, TOP,AVAIL,ITEM)
1.If Top=NULL, then write UNDERFLOW & Exit
2. Set Item=Info[Top]
3.Set Temp=Top & Top=Link[Top]
4. Set link[Temp]=avail & avail=Temp
5. Exit
Example 6.4
1.Push BBB
2.Pop
3.Pop
4.Push MMM
Arithmetic Notation
Highest: Exponentiation
Next Highest : Multiplication (*) & Division (/)
Lowest: Addition (+) & Subtraction (-)

The operations on same level are performed from left


to right.
Example 6.5
Suppose we have
2 3 + 5 * 2 2 – 12 / 6
Polish Notation
Infix notaion
A+B , C-D , E*F, G/H
Polish Notation or Prefix notation
+AB, -CD, *EF, /GH
Examples: Convert following into prefix
1.(A+B) * C = ?
2. A + (B*C) = ?
3. (A + B) * (C- D) = ?
4. 4+3/2*(2+3)=?
Reverse Polish notation or Postfix Notation (suffix)
AB+ , CD- , EF* , GH/
Example:
4+3/2*(2+3)=?
Evaluation of Postfix Expression
Algorithm 6.5: Finds VALUE of arithmetic expression P written
in Postfix notation
1. Add a right parenthesis “)” at the end of P.
2. Scan P from left to right and repeat step 3 & 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 & B is the next-to-top
element
(b) Evaluate B (×) A
(c) Place the result of (b) back on STACK
Evaluation of Postfix Expression
5. Set VALUE equal to the top element on STACK
6. Exit
Example 6.6
P: 5, 6, 2, +, *, 12, 4, /, -
Simulate Algorithm 6.5
Verify by Equivalent infix: ?
Solved Problem 6.9
Consider P: 12, 7, 3, -, /, 2, 1, 5, +, *, +
1. Convert to Infix?
2. Solve?
3. Simulate algorithm 6.5?
Transforming Infix into Postfix Exp
Algorithm 6.6: Polish (Q,P) Q is in infix, this algorithm find
its equivalent postfix expression P
1. Push “(“ onto STACK, & 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 left parenthesis is encountered, push it onto STACK
5. If an operator (×) is encountered then:
Transforming Infix into Postfix Exp
(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.
6. If the 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 [do not add “)” to P]
7. Exit
Example 6.7 modified
Q: A + ( B * C – ( D / E F))
Simulate algorithm 6.6
Example 6.7
Q: A + ( B * C – ( D / E F)*G)*H
Simulate algorithm 6.6
Infix to postfix (Solved Problem 6.7)

(a) (A – B ) * ( D / E) = ?
(b) (A+ B D ) / ( E – F ) + G =?
(c) A * ( B + D) / E – F * ( G + H/K) = ?
Infix to prefix
Solved Problem 6.11
(a) (A – B ) * ( D / E) = ?
(b) (A+ B D ) / ( E – F ) + G =?
Quick Sort, An Application of Stacks

 Quick Sort works on Divide and Conquer Rule


 Quick Sort Strategy is to Divide a List or Set into
Two Sub-Lists or Sub-Sets.
 Pick an Element, Called a Pivot, from the List.
 Reorder the List so that all Elements which are Less
than the Pivot come Before the Pivot and so that
All Elements Greater than the Pivot come After it.
After this Partitioning, the Pivot is in its Final
Position. This is called the Partition operation.
 Recursively Sort the Sub-List of Lesser Elements
and the Sub-List of Greater Elements.
44 33 11 55 77 90 40 60 99 22 88 66
Starting from the Right to Find the Number < 44
22 33 11 55 77 90 40 60 99 44 88 66
From the Left to Find the Number > 44
22 33 11 44 77 90 40 60 99 55 88 66

22 33 11 40 77 90 44 60 99 55 88 66

22 33 11 40 44 90 77 60 99 55 88 66
Now 44 is on its Correct Position
 We can Process only One Sub-List at a Time
 Two Stacks Lower and Upper will be used
 Boundary Values :
 Address of the First and Last values of Sub-List
Lower 1 Upper 12
Lower Empty Upper Empty
Lower 1,6 Upper 4,12
Lower 1,6 Upper 4,10
A[6] A[7] A[8] A[9] A[10] A[11] A[12]
90 77 60 99 55 88 66
66 77 60 99 55 88 90
66 77 60 90 55 88 99
66 77 60 88 55 90 99
First Sub-List Second Sub-List
QUICK ( A, N, BEG, END, LOC )

 A : Name of Array
 N : Number of Elements
 BEG : Beginning Boundary Value
 END : Ending Boundary Value
 LOC : Position of the First Element
 Local Variables :
 LEFT

 RIGHT
Procedure 6,7 Quick (A, N, BEG, END, LOC)

1. [Initialize] Set LEFT := BEG, RIGHT := END and LOC := BEG


2. [Scan from Right to Left]
a) Repeat while A[LOC] <= A[RIGHT]
RIGHT := RIGHT – 1
[End of Loop]
b) If LOC = RIGHT, then : Return
c) If [LOC] > A [RIGHT] ,then:
1) [Interchange A [LOC] and A[RIGHT] ]
TEMP := A[LOC], A[LOC] = A[RIGHT],
A[RIGHT]=TEMP
2) Set LOC := RIGHT
3) Go to Step 3
[End of If Structure]
3. [Scan from Left to Right]
a) Repeat while A[LEFT] <= A[LOC]
LEFT := LEFT + 1
[End of Loop]
b) If LOC = LEFT, then : Return
c) If [LEFT] > A [LOC] ,then:
1) [Interchange A [LEFT] and A[LOC] ]
TEMP := A[LOC], A[LOC] = A[LEFT],
A[LEFT]=TEMP
2) Set LOC := LEFT
3) Go to Step 2
[End of If Structure]
Quicksort Algorithm

1. [Initialize] TOP := 0
2. [PUSH Boundary values of A onto Stacks when 2 or More Elements]
If N > 1, then TOP:=TOP+1, LOWER[1]:=1, UPPER[1]:=N
3. Repeat Steps 4 to 7 while TOP != 0
4. [Pop Sub-List from Stacks]
Set BEG := LOWER[TOP] , END := UPPER[TOP]
TOP := TOP -1
5. Call QUICK (A, N, BEG, END, LOC)
6. [Push Left Sub-List onto Stacks when 2 or More Elements]
If BEG < LOC - 1 then TOP := TOP + 1, LOWER[TOP] := BEG,
UPPER[TOP] := LOC – 1 [End of If Structure]
41

7. [Push Right Sub-List onto Stacks when 2 or


More Elements]
If LOC + 1 < END then TOP := TOP + 1,
LOWER[TOP] := LOC + 1,
UPPER[TOP] := END [End of If Structure]
[End of Step 3 Loop]
8. [Exit]
Quiz # 2
 Write a procedure DELLAST (INFO,LINK,START) that
deletes the last node in a linear list.
 Suppose LIST is a header circular list in memory.
Write an algorithm which deletes the last node from
list.

Potrebbero piacerti anche