0% found this document useful (0 votes)
7 views32 pages

Stack

Uploaded by

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

Stack

Uploaded by

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

Data Structures and Algorithms

Stack

Prepared By : Vaishali Koria Data Structure and Algorithms 1


Stack Operations
Push(Element) - Insertion int st[5];
Top=-1
Step 1: If Top=Max-1
Max=5
Print “Overflow : Top=-1
Stack is full” and Exit
End If Element Index
Step 2: Top=Top+1 Push(20) 20 0 Top=0
Step 3: Stack[TOP]=Element
Push(30) 30 1 Top=1
Step 4: End
Push(40) 40 2 Top=2
Push(50) 50 3 Top=3
Push(60) 60 4
Top=4
Push(70)
Top=Max-1=4(5-1)
“OverFlow”

Prepared By : Vaishali Koria Data Structure and Algorithms 2


Stack Operations
Pop() – remove element from
stack
1. [Check for the stack Pop() “Underflow” Top=-1
Underflow]
If Top == -1 then Element Index
Print “Stack Underflow” Pop() 0 Top=0
20
and Exit
Pop() 30 1 Top=1
else
[Remove the top element] Pop() 40 2 Top=2
item=Stack [Top] 3
Pop() 50 Top=3
[Decrement Top by 1]
Top=Top-1 Pop() 60 4
Top=4
Return item
2. Exit

Prepared By : Vaishali Koria Data Structure and Algorithms 3


Tower of Hanoi

1
2
3
Source = ‘A’ Intermediate = ‘B’ Destination = ‘C’

TOH is a mathematical puzzle which consists of three towers named as origin,


intermediate and destination and more than one disks. These disks are of different
sizes and the smaller one sits over the larger one.

In this problem we transfer all disks from origin tower to destination tower using
intermediate tower for temporary storage and move only one disk at a time.

Prepared By : Vaishali Koria Data Structure and Algorithms 4


Tower of Hanoi

1
2
3
Source = ‘A’ Intermediate = ‘B’ Destination = ‘C’

Algorithm for TOH problem:


Let’s consider move ‘n’ disks from source tower (A) to destination tower (C), using intermediate tower (B) as
auxiliary.
1. Assign three tower A, B & C
2. If n==1
Move the single disk from A to C and stop.
3. If n>1
a) Move the top (n-1) disks from A to B. (source to intermediate)
b) Move the remaining disks from A to C (source to destination)
c) Move the (n-1) disks from B to C (intermediate to destination)
4. Terminate
Prepared By : Vaishali Koria Data Structure and Algorithms 5
Tower of Hanoi
a) Move the top (n-1) [2] disks from A to B. (source to intermediate)

1
2
3
Source = ‘A’ Intermediate = ‘B’ Destination = ‘C’

2 1st disc A->C


3 1
Source = ‘A’ Intermediate = ‘B’ Destination = ‘C’

3 2 1 2nd disc A->B


Source = ‘A’ Intermediate = ‘B’ Destination = ‘C’
Prepared By : Vaishali Koria Data Structure and Algorithms 6
Tower of Hanoi
a) Move the top (n-1) [2] disks from A to B. (source to intermediate)

1
3 2 1st disc C->B
Source = ‘A’ Intermediate = ‘B’ Destination = ‘C’

Prepared By : Vaishali Koria Data Structure and Algorithms 7


Tower of Hanoi
b) Move the top nth disc [3rd] disks from A to C. (source to destination)

1
2 3 3rd disc A->C
Source = ‘A’ Intermediate = ‘B’ Destination = ‘C’

Prepared By : Vaishali Koria Data Structure and Algorithms 8


Tower of Hanoi
c) Move the (n-1) [2] disks from B to C. ( intermediate to destination)

1
2 3
Source = ‘A’ Intermediate = ‘B’ Destination = ‘C’

1st disc B->A


1 2 3
Source = ‘A’ Intermediate = ‘B’ Destination = ‘C’

2
1 3 2nd disc B->C
Source = ‘A’ Intermediate = ‘B’ Destination = ‘C’
Prepared By : Vaishali Koria Data Structure and Algorithms 9
Tower of Hanoi
c) Move the (n-1) [2] disks from B to C. ( intermediate to destination)

2 2nd disc B->C


1 3
Source = ‘A’ Intermediate = ‘B’ Destination = ‘C’

1
2 1st disc A->C
3
Source = ‘A’ Intermediate = ‘B’ Destination = ‘C’

Prepared By : Vaishali Koria Data Structure and Algorithms 10


Tower of Hanoi
TOH(3,’A’,’B’,’C’)

TOH(2,’A’,’C’,’B’) 3rd disc A->C TOH(2,’B’,’A’,’C’)

TOH(1,’A’,’B’,’C’) 2nd disc A->B TOH(1,’C’,’A’,’B’) TOH(1,’B’,’C’,’A’) 2 disc B->C


nd TOH(1,’A’,’B’,’C’

1st disc A->C 1st disc C->B 1st disc B->A 1st disc A->C

Prepared By : Vaishali Koria Data Structure and Algorithms 11


Conversion of Expression
Types of expressions:

Validity of expressions:

Need for conversion:

Prepared By : Vaishali Koria Data Structure and Algorithms 12


Conversion of Expression
(I) A+B*C

(II) A+B*C+D

Prepared By : Vaishali Koria Data Structure and Algorithms 13


Conversion of Expression
(III) (A+B)*C

(IV) A+B*(C+D)

Prepared By : Vaishali Koria Data Structure and Algorithms 14


Conversion of Expression
(I) A + ( B * C - ( D / E ^ F ) * G ) * H

Prepared By : Vaishali Koria Data Structure and Algorithms 15


Conversion of Expression
(I) A + ( B * C - ( D / E ^ F ) * G ) * H

Prepared By : Vaishali Koria Data Structure and Algorithms 16


Conversion of Expression
(I) a * b + (c –d ) + ( ( e * f) * g +h ) ^ i

Prepared By : Vaishali Koria Data Structure and Algorithms 17


Conversion of Expression- Algorithm
UNPARENTHESIZED_SUFFIX

Purpose: To convert infix expression to postfix format

Inputs:

• Input string name- INFIX


• Precedence and rank table- f and g
• String function- NEXTCHAR to return next character
• NEXT – token to examine Symbol Precedence (f) Rank(r)
• TEMP – unstacked element +,- 1 -1
• Input string is padded with # on the right.
*,/ 2 -1
variables 3 1
# 0 -

Prepared By : Vaishali Koria Data Structure and Algorithms 18


Conversion of Expression- Algorithm
1.[Initialize the stack] 7.Remove remaining elements
TOP=0 from stack
Repeat while S[TOP]!=‘#’
S[TOP]=‘#’
TEMP = POP(S,TOP)
2. [Initialize output string and rank count] POLISH = POLISH o TEMP
POLISH= ‘’ RANK = RANK + r(TEMP)
RANK=0 If RANK<1
3.[Get first input symbol] Then write(‘Invalid’)
NEXT=NEXTCHAR(INFIX) Exit
8.[Is expression valid?]
4.Repeat through step 6 while NEXT!=‘#’
If RANK=1
5.[Remove symbols with greater or equal Then Write(‘valid’)
precedence from stack] Else
Repeat while f(NEXT)<=f(s[TOP]) Write(‘Invalid’)
TEMP = POP(S,TOP) Exit
POLISH = POLISH o TEMP
RANK = RANK +r(TEMP)
If RANK<1
THEN WRITE(‘Invalid’)
Exit
6.[Push current symbol onto stack and
obtain next input symbol]
Call PUSH(S,TOP,NEXT)
NEXT=NEXTCHAR(INFIX)
Prepared By : Vaishali Koria Data Structure and Algorithms 19
Conversion of Expression- a*b+c#
Character Scanned Stack Output Rank Symbo Precedenc Rank
l e (f) (r)
# +,- 1 -1

a #a *,/ 2 -1
variabl 3 1
* # a 1 es
# 0 -
#* a 1
b #*b a 1
+ #*b a 1
+ #* ab 2
#+ ab* 1
c #+c ab* 1
# #+c ab* 1
# ab*c+ 1

Prepared By : Vaishali Koria Data Structure and Algorithms 20


Example: a+b*c/d/e+f^g

Prepared By : Vaishali Koria Data Structure and Algorithms 21


Conversion of Expression- With Parenthesis
•Step 1: Add '')" to the end of the infix expression
•Step 2: Push ( onto the stack
•Step 3: Repeat until each character in the infix notation is scanned
• IF ‘(‘ is encountered, push it on the stack
• IF an operand (whether a digit or a character) is encountered, add it postfix expression.
• IF a ")" is encountered, then
a. Repeatedly pop from stack and add it to the postfix expression until a "(" is encountered.
b. Discard the "(". That is, remove the(from stack and do not add it to the postfix expression
• IF an operator is encountered, then
a. Repeatedly pop from stack and add each operator ( popped from the stack) to the postfix
expression which has the same precedence or a higher precedence than Operator
b. Push the operator to the stack
•[END OF IF]
•Step 4: Repeatedly pop from the stack and add it to the postfix expression until the stack is empty
•Step 5: EXIT

Prepared By : Vaishali Koria Data Structure and Algorithms 22


Example : (a+b) * (c+d)
Character Scanned Stack Output Rank
(
( ((
a (( a 1
+ ((+ a 1
b ((+ ab 2
) ( ab+ 1
* (* ab+ 1
( (*( ab+ 1
c (*( ab+c 2
+ (*(+ ab+c 2
d (*(+ ab+cd 3
) (*(+ ab+cd+ 2
(* ab+cd+* 1
Prepared By : Vaishali Koria Data Structure and Algorithms 23
Example : (a+b) *c+( d/e )

Prepared By : Vaishali Koria Data Structure and Algorithms 24


Infix to prefix conversion: a+b*c
(I) Reverse the expression : c*b+a

Prepared By : Vaishali Koria Data Structure and Algorithms 25


REVPOL
Purpose: To convert infix expression to postfix format

Inputs:

Input string name- INFIX


Precedence and rank table- f and g
String function- NEXTCHAR to return next character
NEXT – token to examine
TEMP – unstacked element
Input string is padded with ) on the right.
Symbol Input Stack Rank(r)
Precedence (f) Precedence (9)

+,- 1 2 -1
*,/ 3 4 -1
variable 7 8 -1
( 9 0 1
) 0 - -
Evaluation of postfix expression
Eg., 2 3 +

Prepared By : Vaishali Koria Data Structure and Algorithms 27


Evaluation of postfix expression
Eg., 5 6 7 * +

Prepared By : Vaishali Koria Data Structure and Algorithms 28


Evaluation of postfix expression
Eg., 5 3 + 8 2 - * Eg., 1 2 4 ^ ^

Prepared By : Vaishali Koria Data Structure and Algorithms 29


Evaluation of postfix expression
Eg., 18 3 / 2 ^ 13 7 + 5 2 ^ * +

Prepared By : Vaishali Koria Data Structure and Algorithms 30


Evaluation of prefix expression
Eg., - 6 5 Eg., - + 8 / 6 3 2

Prepared By : Vaishali Koria Data Structure and Algorithms 31


Do the following:
Convert the following expression into prefix and postfix and evaluate for this values
in both the forms:
a*(b^c^d) +e-f/g , a,c,d,g= 2 b=1, e= 10, f=4

Prepared By : Vaishali Koria Data Structure and Algorithms 32

You might also like