You are on page 1of 10

Recursion uses stack STh

↳ Life : Last In First out

It is a collection of elements that follow LIFO

for insertion and deletion .

ADTStack ( Abstract data t


epp
Implementation of stack
using
Data : I .

Space for
storing elements I .

Array
list
2 .

Top pointer 2 . Linked

I
Operations : .

pushtu )
2 C )
pop
-

3 .

peek Linden )
u . StackTopi )

C)
5 .
is
Empty
6 .
is Full 1)

IMPLEMENTATl0N0FSTACl#NGARRAY

Struct stack

{
int size ;
St
int top ;

SUITE
-

* size
int s
;
}; top

O l 2 3 4
int Maini ) -
I

top
{
struct stack St
;
);
'' "
fl Enter of stack
print size
''
"
scarf ( 1. d Lst );
'
-
size
,

St -
s =
new int Est - size ) ; for heap memory
St -

top = -
l ;
}
Stack empty if ( top 17
pod 011)
- -

stack full -

if ( top ==
size -
i)

int pop ( stack


-
St )

PUSH 0117 E
intr -_ -
l
;
*
void push ( stack St ,
intr )

{ if ( St → t)
top
-
==

if ( top St ) "
St → l
psintfl Underflow
→ size );
"
==
stack
-

"
Overflow
''
);
printfl stack else
else {
{ n= St → s Est →
top ] ;
St toptt ;
→ st →
top - -

;
St → s Est →
top ] =n ; }
} return n ;
} 3

/
peeked pos Inder =
Top -

post )

igntpeencstackst.int post L z
int l 3 I
n =
;
-

0
y

if ( St -

top -

post I do )

Position ) ;
" "

print fl Invalid-

else isEmp
n= St -
s [ St -

top -

post , ] ; int is Empty ( stack St )

return n if ( St -

top
-- =
l )
;
-

} return 1 ;

else

return 0
;
Stacktopll 3

int stack top ( stack St ) isF


{ int is Full ( stack St )

if 1st top -7=-1 )


-

{
return -
l
; if ( St -

top
-- = -
I )
else return 1 ;
return St s
-

Est top ] ; -

else

} return 0
;
}
STACK USING LINKED LIST
#

NODE
Empty : if ( top == NULL )

#
-

struct Node Full : Node At =


new Node ;
hent
{ data if Lt== NULL )

int data ;
struct Node *
neat ;

5→o→%4o
T
ft
Top Oln )
ol )
'

insertion

PUSH popes

void push ( int n ) int pope )


{ {
*
Node At = new Node ; Node p;

int n = -
l
;
if ( t= -

- NULL )
"

psintfl stack Overflow


''
); if ( top ==
NULL )

);
" "

else printfl Stack is empty


{ else

t → data =u ; {

+ → ment top p top ;


-
=
;
-

top =t ; top =
top → neat ;

} n
=p → data ;

}
free Cp) ;
}

peeked return n
;
}

int Peek ( int post

/
{ if C p ! = NULL )

int i ; return p → data ;


*
Node p= top ; else

return -
i;

go , ,i=o;p , .nu , ,, , .ms . .


, ; .,+ , ,

p =p → next ;
Stacktopc )
#
is Full
#

int stack top C ) int is Full )

{ 9
ifltop ) Node *
t -
new Node ;

return top → data ; int r - t ? I :O ;

return -1
; free ( t) ;
} return r
;
}

isfmpty

int is Empty " false


{ I
return Top ? O : l ;

↳ True
}

paoanthesisM-atchingexplc.IE#
int is Balance ( char temp)
{
Struct stack St ;

of stack
St size stolen temp) ;
initializing
]
.
=

St .

top = -
l
;
St -

s =
new char [ St -
size ] ;

Ascii
for Li :O ;
' '

enp[ i] ! = 10
; it -1 )

{ ( 40

if (
'
C
'
) ) 41
enp[ i ]==
E
push ( Lst , emptied ) ; 91

if (
' '

) I 93
else emptied = = I

if ( is Empty ( St ) ) { 123

return false ; } 125

else

pop ( Ist ) ;
}
1st ) ? true false ;
return is
Empty :

}
INFIXTOPOS.TT/XCONVERSlO#

I .
What is post fin
2 .

Why post fin


3 .
Precedence
4 .
Manual Conversion

1.
Infix :
Operand operator Operand
atb

2. Prefix :
Operator operand operand
tab SYMBOL PRECEDENCE ASSOCIATIVITY
l L
+ , -
-
R

Postfix I
*
3- :
Operand operand operator ,
2 L -
R
abt A 3 R -
L

4 R
#
L
-
-

5
-
Unary minus C) L -
R

Eg at b c
=

( ( bk) ) First it
at
fully paoanthecise .

- -
prefix postfin

( at be] ) ( at [be ] ) .

[+ a. be] [abc t] -

ASSOCIATIVITY

- #
Left to
Right Right to left

at btc - d a=b=c= 5

(( ( atb ) + c) -
d ) ( a -
( b -1=5 )) )
y
Poweropeoatorenample Unooyopeoatorstnample
'
b' c

! ! :p
'" ' "

gig! : .ie?ationr-
a a

go
.

cane . " , . .

Postfin : ( a^[bcD ) ( -
f- a) ) ( al -
p)
)
abcnn

(3) n ! (4) toga


in pre toga
pre : :

!
post : n post :
aloof
\

- !
Example : at b
login
-

log En ! ]
-
at b
-

-
En
at b !
log ]
-

[a) t b En ! log ]
-

[a -3 tfbn ! log'D
a bn !
log 't
-

INFIXTOPOSTFIXCONVERS.IO#

At b * c -
d le SYMBOL PRECEDENCE ASSOCIATIVITY
l L
+ , -
-
R

Symbol Postfix I
*
Stack ,
2 L -
R
a a

t t a

b t ab
* re
,
t ab
C A t abc
,

abc At
-
-

d -
abcttd
I 1, -
abc * td
e 1, -

abc * + de

abc * + del -
Ans
PROGRAMIN
fix a t b A c -
d
6
l
7
e

8
lo

9
O l 2 3 4 5

*
Chao convert C char *
infin )
{
Struct stack St ; 11 Initialized
[ stolen linfintl ))
*
char postfix =
new char ;

int i- o
;j=o ; for null
string
' '
while C infirm [i ] ! = lo )

{
if ( is
operand ( infiniti ]) )
postfix [jtt] infirm [itt] ; =

else

{
if ( pre linfin [ i ] ) pre ( stack top ( St ) )
>

push ( Lst , infiu [ it -13 ) ;


else

postfix Ejtt ] pop Cdt ) ;


=

}
} -

/ .in/
int ( charm )
pre
while ( ! is Empty 1st ) ) {

postfix Tjtt] =
pop ( Ist) ; if I n==
'
t 'll n ==
'
-
l )

:÷÷÷: ;
"
" "
so:*
else
'
"

, return 0 ;

-
int is operand ( char n )

{
l'
)
'

Il
'
' ' l ' ' '

if ( n l
l
'
==
== t n e- = -

, , z * , , n ==

return 0
;
else

return 1 ;

}
( ( at b) * ) d^e^f
¥ c SYMBOL OUT STACK IN STACK
-

PRE PRE
( [ abt] *
c ) -
dnenf

[abt CA) -
d ne tf t, - I 2

[abtc* ] -
d^Eefn] P
,
I 3 4

[abt -
J [defm] A 6 5

)
c
-

C
abt ctdefnn
70 ?
-

Chosin
g bracket
F R -
because of
L
V

associativity
into
cannot be
pushed
stack

EVALUATION OF POSTFIX
#

35*62/+4 -

SYMBOL STACK OPERATION

3 3

5 5,3
*
5*3
15

6 6,15
2 2,6 ,
15

I 612

3,15
T 15+3

18

4 4,18
-

18 -
4

-
N =
6+5-13*4
*
U = 65+34 +

*
Here
#gets executed first instead of *
because precedence and

associativity are meant for paoenthecisation , they don't decide which

operator gets executed


first .
PROGRAMFOREVALU.AT/0NOFP0STt#

*
postfix 3 5 6 2 I t 4 IO
-

O l 2 3 4 5 6 7 8 9

)
Eval ( char
post fin
int *

{
struct stack St ;

int i ni , Nz it ;
,

[ i3 !
'

for
'
l i=o postfix = lo
; itt )
;
{

if l is operand ( postfix [i3 ) )

push ( Ist , postfix) ;


else because operand will be pushed
{ into the stack in its ASCII

uz
=
pop ( Ist ) ; value because postfix expression
n
pop ( Ist ) ; is in Chao
=
, .

Fooeg : 3
'
'

postfix [i ] )
I '
switch ( 51 -
48 =3

{
' '
case t : r =
n , tnz ; push ( Ist ,r ; break ;

push ( Ist ,r
'
case
'
-
: o = n, -

uz ; ; break ;
' '
*
push ( Ist ,r break ;
t
case : r n, ;
nz ;
=

'

push ( Ist ,r
'
case l : r =
n , lnz ; ; break ;

}
}

)
return pop ( Ist ;
}

You might also like