Lecture 3 DSA
Lecture 3 DSA
• Other names
2
R E AL L I FE SCE NAR I OS
• Books on floor
• Dishes on a shelf
• In programming, consider doing X = (A+B) * (C+D)
3
STACK ADT OP E R ATI ONS
4
STACK ADT OPE R AT I ONS
5
STACK ADT OP E R ATI ONS
• CreateStack(S)
• Make Stack S be an empty stack
• Top(S)
• Return the element at the top of stack S
• Pop(S)
• Remove the top element of the stack
• Push(S,x)
• Insert the element x at the top of the stack
• Empty(S)
• Return true if S is an empty stack and return false otherwise
6
PUSH AND P OP OP E R ATI ONS OF STACK
7
AP P L I C ATI ONS
• Parsing code
• Reversing a string
• Backtracking in Depth-First-Search
8
USE OF STACK I N FUNCT I ON C AL L S
• Local variables
9
USE OF STACK I N FUNCT I ON C AL L S
10
RU NT I ME STACK E XAMPL E
void main(){
int a=3;
f1(a); // statement A
cout << endl;
}
void f1(int x){
cout << f2(x+1); // statement B
}
int f2(int p){
int q=f3(p/2); // statement C
return 2*q;
}
int f3(int n){
return n*n+1;
}
11
RUNT I ME STACK
Function
Return value Local
Parameters value variables Return address
13
STACK L I B R ARY ( J AVA)
14
STATI C AND DY NAMI C STACK S
linked lists
15
AR R AY I MPL E MENTAT ION – FI R ST SOL UT I ON
Empty
maxlength
16
AR R AY I MPL E MENTAT ION – FI R ST SOL UT I ON
Problem
• Every PUSH and POP requires moving entire array up and down
• Fixed size
231
2
1
1
…
17
AR R AY I MPL E MENTAT ION – T W E AK E D SOL UT I ON
Empty
maxlength
Idea
• Anchor the bottom of stack at the start of array
• Let the stack grow towards end of array
• Top indicates current position of recently inserted stack element
18
USI NG STACK ( C+ +)
19
USI NG STACK ( J AVA)
20
T H E TOW E R S OF H ANOI
A STACK - B ASE D AP P L I C ATI ON
23
TOW E R S OF H ANOI
24
TOWE R S OF H ANOI
25
TOWE R S OF H ANOI
26
TOWE R S OF H ANOI
27
TOWE R S OF H ANOI
28
TOWE R S OF H ANOI
29
TOWE R S OF H ANOI
30
TOWE R S OF H ANOI
31
AL G E BR AIC E XP R E SSIONS
32
ASSOCI ATI V I T Y OF OP E R ATOR S
() Parentheses (function call)
[] Brackets (array subscript)
. Member selection via object name left-to-right
-> Member selection via pointer
++ – – Postfix increment/decrement
++ – – Prefix increment/decrement
+– Unary plus/minus right-to-left
!~ Logical negation/bitwise complement
* / % Multiplication/division/modulus left-to-right
+ – Addition/subtraction left-to-right
<< >> Bitwise shift left, Bitwise shift right left-to-right
< <= Relational less than/less than or equal to
left-to-right
> >= Relational greater than/greater than or equal to
== != Relational is equal to/is not equal to left-to-right
&& Logical AND left-to-right
|| Logical OR left-to-right
?: Ternary conditional right-to-left
= Assignment
+= -= Addition/subtraction assignment
*= /= Multiplication/division assignment
right-to-left
%= &= Modulus/bitwise AND assignment
^= |= Bitwise exclusive/inclusive OR assignment
<<= >>= Bitwise shift left/right assignment 33
I NFI X, POST FI X AND P R E FI X E XP R E SSI ONS
• Infix
• Expressions in which operands surround the operators
• Example: A+B-C
34
E XAMPL E: CONV E R SI ON FROM I NFI X TO POST FI X
• Infix: A+B*C
35
E XAMPL E: CONV E R SI ON FROM I NFI X TO POST FI X
36
I NF I X , POST FI X AND P R E FI X E X P R E SSI ONS E X A MP L ES
A-B/(C*D^E) ? ?
37
W H Y DO W E NE E D P R E FI X AND P OST FI X ?
38
W H Y DO W E NE E D P R E FI X AND P OST FI X ?
39
E XPR E SSION E VAL UATI ON ( MAJ OR CH AL L ENGES)
40
E XAMPL E: POST FI X E XP R E SSIONS E VAL UATION
41
E X AMP L E: P OST F I X E X P R E SSIONS E VA L UAT ION
A ND U SE OF STAC K
42
E XAMPL E: POST FI X E XPR E SSIONS E VAL UAT ION
AND USE OF STACK
A. 18
B. 36
C. 24
D. 11
E. 30
43
E VAL UATING A P OST FI X E XP R E SSI ON
45
E VAL UATING A P OST FI X E XP R E SSI ON
46
I NFI X TO POST FI X ( W I T H OUT PAR E NT H E SIS)
• Token is an operand
• Append it to the end of postfix string
• Token is an operator, *, /, +, or –
• First remove any operators already on the opstk OR stack that have higher
or equal precedence and append them to the postfix string
• Push the token on the opstk OR stack
• Example: 4 - 2 + 6 * 2
47
CONV E R SI ON OF I NFI X TO POST FI X
• Precedence function
• prcd(op1, op2)
• op1 and op2 are characters representing operators
• Examples
• prcd(‘*’ , ’+’) returns TRUE
• prcd(‘+’ , ’+’) returns TRUE
• prcd(‘+’ , ’*’) returns FALSE
48
AL G OR I T HM TO CONV E RT I NFI X TO POST FI X
Example: A+B*C
symb Postfix string opstk
A A
49
AL G OR I T HM TO C ONV E RT I NFI X TO P OST FI X
Example: A+B*C
50
AL G OR I T HM TO C ONV E RT I NFI X TO P OST FI X
Example: A+B*C
symb Postfix string opstk
A A
+ A +
B AB +
51
AL G OR I T HM TO C ONV E RT I NFI X TO P OST FI X
Example: A+B*C
symb Postfix string opstk
A A
+ A +
B AB +
* AB +*
C ABC +*
ABC* +
ABC*+ 52
I NFI X TO POST FI X ( PR ACT I CE)
Example: A*B+C
symb Postfix string opstk
53
I NFI X TO POST FI X CONV E R SION ( P R ACT I CE )
Example: A*B+C
symb Postfix string opstk
A A
* A *
B AB *
+ AB* +
C AB*C +
AB*C+ 54
I NFI X TO POST FI X ( WI T H PAR E NT H E SIS)
• Token is an operand
• Append it to the end of postfix string
• Token is a left parenthesis
• Push it on the opstk
• Token is a right parenthesis
• Pop the opstk until corresponding left parenthesis is removed
• Append each operator to end of postfix string
• Pop the left parenthesis from stack [opstk] and discard it as well
• Token is an operator, *, /, +, or –
• Push it on the opstk
• First remove any operators already on opstk that have higher or
equal precedence and append them to postfix string
• Input expression has been completely processed
• Any operators still on opstk can be removed and appended to end
of postfix string 55
W H AT I F E XPR E SSION CONTAI NS PAR E NT H E SI S ?
R E QUI R ED AL G OR I TH MIC CH ANG ES
• As you will never push closing parenthesis in the stack, so, this case will
never be encountered
56
AL G OR I T HM TO C ONV E RT I NFI X TO P OST FI X
Example: (A+B)*C
symb Postfix string opstk
57
AL G OR I T HM TO C ONV E RT I NFI X TO P OST FI X
Example: (A+B)*C
symb Postfix string opstk
( (
A A (
+ A (+
B AB (+
) AB+
* AB+ *
C AB+C *
AB+C* 58
I NFI X TO POST FI X CONV E R SION ( P R ACT I CE )
59
I NFI X TO POST FI X CONV E R SION ( P R ACT I CE )
• Example: (A + B) * (B – C)
• )C – B( * )B + A( → (C – B) * (B + A) Reverse infix string
• C B - B A + * Perform infix to postfix conversion
• * + A B - B C Reverse postfix to get prefix expression
61
CONV E R SI ON TO P R E FI X E XP R E SSION
• Example: (A+B^C)*D+E^5
62
END