Data Structures 2
Data Structures 2
ONIT-0
v m ced
in last out
in first-Out-0xdes (LIFo) (ov) Fivst
Pusb
Pop
Peek ®) Top
I S Emphy
1oP Push
Loyt in,
frs gt out
Pop
Inseytion and delebion operakiong occuY at one
end
Pusht) opexätion
Adds dn iten in the gtack. t involveg in e
of steps.
I F he Btack is
Emply called "undes ffow", Incye
top o point nert emphy 8pae.
Adds data element to shack locaion, where top is
Poinhin
hen eturns Phe xesult 19551AoSR
Puh operakon Vcvd are
ToP
LA stact
stact
data)
void push lint
top topt
Stack [Top) daBa
=
Else
is tull \n)
inkerk data, stack
(" ould not
sin f.
Popt) opevation
Atack is
conent while Xemoving the
the
ACcesing
Top-
6 ToP
sta:e
i n int pop (int data) 551AOS%
vmharcit
ifi s emphyt)
else
he daba n");
Printf ( colud not rebieve
Peek opexaio
Peek 1s an opexaion Bn Certaio dbstract dat
stacks
yPes, Specially Bequnial collecions Buch as
Collecion
CA
when We gave peek operaion, it gives top ofnesttk
PeekC) =C
S EmptyC operation
he items 8re popped in the veveised oxder in
is called
undex-flow" condition
Peek B) top e turns top element d stack.
1sEmpb Returns tsue if stack is Empt
Pty elsef
els
Representaion o f a s i c k using Asays S61AoSSS
V m Chand in
stack is a data stsuctuve heut can be representes
A
as an asay of elemede.
Stoe dn Jdeved: list ments.
to
is used
Ao a s r a y
of Stack is one of the
TePsesentahon
tor
an aray
USinq the data
to manaoe
technigues
easy betwe en
an a a y and
diffevence
a majos
But here is
stack
is fiaed
an asvay
Si3e of
no fixed Size
8tack, theve is
While in a number
numbes ofF
of
with
Wi h the
the
is changed
of stack
Since the Si3e
leted to and
fsom it
insestedlo) de
Elenents
be uged to epregent
difference an avay Can
Despite the
fov max kize ; big eough
an avzay
a Btack by takinq
a stack.
to manage
Stack a an aY
40 ToP
3 4 S
13
a3 1 8 1 3 18 400
3 Avay
12
Stack
Foy exarmple
l2,8,21,33,8
We are qiven a 8Back of elemenks:
3 4
S
a a 1 a3 1 4
Avsa
slep Vmch
Push
Push(4)
Top40)
at a(5)
Elemen rs inderted
()
) ( ) (4)
40
Poshl4o)
Step:
+ push (18)
Topl18)
Element is ingeted at a{4
(), ) ) , (3) 4)5)
8 4
push18) ToP
Push (33)
Top33
*lement is insested at a13)
3
3 3 18 40
Push(33) ToP
sep
push (a1)
*Top21
glement is inserted at alaj
a1 33 8 40
Push21) ToP
ster5
* Push (o8)
Top 8
*Element is inketed at ala
33 s 40
push (&)
ToP Vmchandana
step 6
Push (1a)
* Tbp = 1
element is
insested at ai63
3
12 333 1g 4
Pusha) ToP
Display
12 2133 1 4
Top Topt1
stack Top] =X
Algthm for pop ) operahion
Stack is undesftous'.
Rekuxn sBack[top]
Top Top-
Poqsam fp Repsesentakion of tack using o
#include stdio. h>
Include x chyperh
t define
void Pgh (int).tot pop(void):
Void display (void):
if (top- -
void display(void)
if (top : -1)
Printf (sack is empty "
e lse
int main t)
int ch,X
do
Printf (*\n Push \t a. pop \t 3.display ")
Scan f (d', Zch);
Switch (ch)
Scanf (" Yd ", 8 x); Vmelhan.rioe
ush (x)
Cae X Pop);
element 1s d "x);
Printf ( popped
break
Case 3 displayt
love ak
Coye 4 Exit (o)
1
Push 2Pop 3. display
Enter an Element 3
Entex an element 9
if (top- ma1-1))
printf (" stack is ovesflou")
else
stf++top3 - X;
if (top=-1)
Printf (" stack is undevflow');
else
veturn (s+ ltop--]);
int main t )
Back Hacking
Mem31y Manaqement
Convession
Inh o posix
When
of the torm a op b.
The Expresion
Ini Expression
an opex ator
of the form
ab op
The expression
Posth Expsession
toi every Pair of opevands.
an
ã n opesator is followed
step Algbithm
consider the nextelement in the input
step1
stpa 1f it is operänd, display it
insest it into
step 3; If it is opening paventhe sis C,
Stack
Shep 4 F it is an opesaBor , then if Btack is
emp
insest the opevatov on stack.
o pesatos to oútput.(1 5
Emphy - highe pi
=next priovty
lowey
3 3 Prioviy
33*
33*
33t4
/
33* 4
-
33*4I
33 41-
334-
3341-l6
6
+* 33*41 /63
33*1-/6
paaxaY
handan
#include Stedio hs
include * chype-h»
chav stack 1o o3
int top -1;
void Push (charx
chay poP C)
if Ctop-z -1)
etusn 12
else
vetuvn. stack [ top-
ir (x =(')
selusn o
veun 1
seuzna;
yen 0
int moinl)
push(e)
else if ( x e = = ' ) ' )
e lse
e++
Cotes he expoesion 33 * 4 - 6a
ResultIS
EValuahng Axithematic Ep session 1955/oSS3
ochardao
The stäck 3igani s lion is vevy effechve in evaluaking
ithemaic expses si
ons EXpyessions ase sualy ve prexgnted
not ion I n lhich ¬ach operatoY
in ulhat Is Known aß inkiz
ab+
The psoeduse fos getting the rezult in tolouing Bteps.
(6+4)
Infs nodakon (3-+4) *
+46 +*
Pest-k notahion 4
Result: 6o
6 6o
Alqaithm
Add) b postfir Expression
encountered
End
3 F ope3and is Encoun teved, Push it into St
)A Top elemet
3 B neat Element
3 Evaluak B ofrrator A
int stmav 3;
void push lint x)
if (t =ma1-1)
Printf oves Flous);
else
stS++t3=X;
in Pop (void)
Ct-1)
Printf (undexftow")
else
selusn (st[t--3);
chas c
while (expti7!='\0')
C expti3
it (is digit Cc))
pushCc-48)
else
op2 PopO,
OPl- Pop);
Switchle)
$5 53
GVm ha
(oPI+0P2); break;
Coge'4' Push
break;
Cae push (op1-òp)
(oPI6p2); break
Cose': push
break
Case' Push (opl/opr) i
loPIaop), brek
Case'a': Push
defat Pzintf t" nvakd expsession )
ve hun CPOPt));
int main ()
Result6o