0 ratings0% found this document useful (0 votes) 24 views31 pagesUnit III Stack and Queue - Watermark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
[oki whot¥s doto glaucure?
collect/store the dalo »
llowonise the data
Qcess the data.
manupwate the data .
= Slack is lagi in first out Clio) os peveh tn lash
Our Cato)
P gt is an ‘ovderj let of the cope bye of clemenis.
i
= A stock fe a Mnear [Pot wheve all inseatchio n and
I| deletion ove permiled only anyone end of the lfo+
rWhen elements ave added. to gtack i grows at one
end_similavly when elements are deleted From oa ~
Slack Th stwick alaione end-
Figure shows, expand and Shreenking of Stack
Tope
ED
~Iniialy ‘elack % emply I
3
if f’ ‘ ie ] Toure
patel eee ‘| tep= =!
I rr figure of insexion, OF (1213 Im Slack.
| Poe ZX POP.
2 | vops2
mwatco 2 [Toes [topo
+ 0 -
Bere ; mos p Vrain Top: 7!
= ===
@ scanned with OKEN Scanner7e avarioble Lop point to%
[Bet ; : -
*|Operalion on a slacks- al
4. TriVal’se CD - Plake a slack empty
2. Empiy ©) - To determing if a stock js empty or)
19. mot T
3. Fall C) - To determine wlack % full ormot
la Push ()- TF ctack fm mol fall then push a new
| alement al the top of the stack Cstmile
+o Insert tn a list)
S-popC) - If Stack fe not empty then pop the
element from it Slack, Csimilor to dalee
from a list)
Write structure from slack using avray é
Fone dimensional avray conbe used to volb clemenia
of 0 Slack : t
Anothee variable “op? 5 used to kept Mock of He
index of the top moét element -
a, ,
Tap —> Stack qrows from left te
maa) x :
‘
@ scanned with OKEN Scanner=A stack a8 Moy be define a8 Followss _
type def stuck stack
int data Cmax);
‘int bop;
% stack 5
j—
—
- Here initfobly top issotd to —\ X
-A stack with top as —! % an empty okack
CF When the value of Lop becomes. mox -1 after sewes
OF inseaxction tts Full afters “push” opesakion top=
top +) Cstack growing) 3 ‘ |
motion top =bop~1 Cétack sirenking)
funchon push.to implement Stack using array
write the push and pop FuncHon stack wating, array
| [Pht max = &
[int stack Eman]
Ob Top = =I
is Fuh)
[it ae
"GE Ctop = = mon 1)
| wekun True 5
ase 4 : |
Tern False 3
Push Cpoia)
@ scanned with OKEN Scanner|| {
| Top= Topt!
| | shock fr6pJ = Dota
| 1 4 os +
| ase. i f
| Lesantiow 2) ———
| is empty C) :
ic .
T
TE C16 $u 07) : ; [
|
Vf CTop= =~!)
! eretuin True) ee eg WS
oa tt ee
| medurn False ’>
4
! pop a
Lint Data
if Clfsempiy )
Data = Stack CTop)
| Top=Top-}
a
ese
q
|
|
| | “underflow” 4 :
4
[Herve top variable fust pointin
A stack:
g§iothe atop of the
@ scanned with OKEN Scanner_cHere adr fe abstract data bypen
A stack is an odes ligt ubith the aeslichton thob
Linseas tion ond deletfon can be perform af only one
tend of Hhe I?6t called Ehe bop-
ane mo
Push equivalent Eo and tneert Pop equivalent to d al eting
the most ¥ecentally fnoert element A pop onan empty |
Stack 6 consider to be an errow a Push operation |
of a stock thats. a full is an,coneides bo bean error
Structure uged from Stack.)
|
|
j
type def struct stack
ins data Cmax )s
ind tap 3
“GOA gtdlcke
?
r Stack data S\ruclure fe very usefalie; ,
F few of Ths Uses are given bdow.
Le
@ scanned with OKEN Scanner| 2” Ex pression evaluation J
3 Passing !
4- Simulogton of Tecurtion
— 7 function cell —_—_—_ |
= There ave three populay method of vepwesentah,
OF an expression
O) rnirix ~X+Yy (operator bet” opevand )
] 5 PrerPK— ry. C operator befose-operand).
©) postPfx - 24+ Coperoto x oFtet, operand’)
EIntix expression ave evaluate ler bo righ} bub
Operator procedance must be Loken into aceount -
For. €.g.- C+ Y*Z
-Hete yond z wil be mulliplying fives and itwil
beadded x
j
for eg. +S#32 Find an operator Prom Bight hI
and perform the operation ;
A Operate is multiply therefore o%2 0%
multiplying, the Second operator is + fhevefow oe
V
@ scanned with OKEN Scannerfind the isl opevoer from the lett to vignt
1n_Fixst operatoy is and thetigee Band
- —
rExpression becomes S6* the second o is 4 Sond
= uh eR
axe added Yherefove expession Bagh es ai.
Given
6 53+gH +
@ scanned with OKEN Scannerposifix expression show all step
-eme
uate Follow’
Fxg iT exolwal followin cs10 , dzis eg
Sec? “i abectd-e+ @heve a-S 64
>| step Expression etack
ere
be Crd=e +
¥ c+ ae
c+d-et
+d-et
att,
The value oF postfix expressfan ie 2)
@ scanned with OKEN Scanner| Evaluate the Follawing postPix ex preesion using stack
6234-382] 4 H2S 34
) step | Expression Stack |
uy © G2D4-3Rt KASH LW 7
| p22, 2AX-BBUAstHWBRL |
4 Ye] i
|
[IPs [ a4- ary k2day +A
4 38014 kK 2534 =
te}
s — 88214 agay i 3 | |
I we fa te
6 S821 +H 0$34 | ai
1 —)
7 Brute 7gda+ | a
g U+HWS st | ic |
[ |
ls {re odo + | fH
Yo TR 8+ |
3 |
nen
u * 23+ i |
tS
ha 2st we
1} SiS
ts 3y
y
@ scanned with OKEN Scanner16 END —
The value of postfix expression is Sa
Evaluation of prefix expressions-
The ~ preFin eRpression is e
@ scanned with OKEN Scanner(B® C+DRE -ARC
Expyession | outpuk-
RIB RCT DREAAC | + —
|
IBEX C+D RE-AKG A
4 d- [eax CHDK E-ARG DLA
[ae [eR CrpR EAR ee
| | |
/ Ss CMD £-Ak&k C AB
[¢- [SDREARE [ABC
+ :
7. DRE-ARG + [eR
8: KEAR C | ABC ¥ TD
3: Bana | [Rack XID
bs Eee Inno R WIDE
Ak C [ABC%% [De t*
|
#C [Rocka [Dere®
fm | = . ARC ee DEtE
Tas “RECEA IDE +
@ scanned with OKEN Scanneri
tr aabe Cos Fave wall Ae
= tb% Cold$ a) jh Nulb> a
[3- [ beer Say 1b { ex a
Ce * Celd gayle yt aw
j= 1 teid say Tb <= ab
€ | clagayle = + abe
Taboo REC — ah
§_asavre RUT late
Sadi FCI abe a
i (orb SECS abel |
" 7b ARETE abcda”
Tabeda
+kCIS)
@ scanned with OKEN Scanneroutput.
cae “expressioo + stack
- a
No oS.
SS ee eee
i %3 ‘ li pokcoa i
——_| ___—__7___[abedod [6
Pose _ +/ abcda$ |
is - = abedadl® 6+) ~
| Thesefore Intix bo posttix exp¥ession 18
abcda gx b+/
@ Identity the expression ae convert tin inte
Temaining to form - :
[ i. ab Poe a FeSh
mheve $ - exponed at
— | Forix expre S100 o-
| = (CA+B) *c -Cd- oe)
Feet Oxpression +- :
2¢ 2 fk +PRC-DErea.
+
at aR
|
| >
|
2. “AieF CEE
InFix expressian Be
[7 = A-Bieewag 6)
Posthix expressiarr:~ :
| | ag cpeg*/- —
ad
@ scanned with OKEN Scanner| Gloe the followin
and preFly.
pax blo xd-ele |
| Di Case yiCrd) = 7
Dhato+cyd
Q infix exprescion Find thety Postfix |
ng_missing expession inthe ible
! i
i akblc xd-eif §_
Torin [bretix | Post tix 1
[core xO Ter) Jestpero/eF Tascawee +] |
=o oeesnestesendictlc cs 8
AERKC AAKHC AB CEE |
fl | / |
PACO+C)-EIF | -HA+BC/EF ABC +e EFI — |
|
|
prefix expression —
~* 1% abcd ler
Post Fix expreasion -
abac id ¥eFle—
WV} Carb) Co+dy
eee
pREIX Expression-
Htastcd |
PostFi x exptession -
abtcd +]
ae —_
@ scanned with OKEN ScannerFdentify he expression
0 Form + oe zo
aktblexd-elFf _ KWN/
Prefix expression = a
—#/* ler 7
4
Postfix expres
G ¥cldeeF [-
beAktep [tabtcd :
|| postFix_expression=
! HB Abt cdt/
@ scanned with OKEN Scanner+9 Convert the given infix expression
(O$ 6) ¥ C-d/g ry stack Lin pestBx axp*
Sa “_|__ Expression _ |
$b)¥c-dd
| &)% C-d ©
d* ¢-d/d
* C-d/d
Cdk ey ——
: q ee
> i) a
d a
Empty -1
Empty Empty
@ scanned with OKEN Scannert
|
|
Convett Fhe given anfir expression to Besifix
| expression
CAB -Cc#D)) [Céx®)
IGx expression ghack oud pur
areal
rs: aT c Empty
{ [a - RIE ONGD Cf a
[= |[B-Cc¥ Dy) 1CE+F hi C*¥ A
| 3 |-Cceo) ETE) Wo an
re EREN LST ae
7 CRDICEFE) | = CBE
8 |) CesFY | -C ABR C
43% |D)) Ce+F.) [=Ck ABEC
| lo" | yy] ere): [=ca AB¥CD
4 ne | ces =C¥)___Tpakep
B. (Ce+t) een
a [Cet Y Emplg aeKCD=*
@ scanned with OKEN ScannerABECD -%e |
Ke is. €+F) Ka ABCD -¥
| 16 +F) IC ABx GD -¥ E
{| | f) C+ ABRCD-KE
18 J LiC+ ABK CD -¥ EF
| 19 Empha C+) ABA CD-*EF
20 | empty GS AG* CD-a EF +]
| {
zt éemply femphy PBKCD- ees]
| | {
| |
| |
|
a
@ scanned with OKEN Scanner—_—.
: post fx expreSston shy
step In tobulag Fotm
Q 4 Evaluate the following
the, stack after, each”
posthx expresolen
POR+wGTI- gwen Pee 4°
Gpreesion ___*
ff | tes ye tote
pepevsrl-
@ scanned with OKEN Scanner@ scanned with OKEN Scanner3
r Comet Tha gen infix expression _4o posi,
— ass ee rte
a on apmesion __|_Stack ouput -
fp no [
a To lenee)-Ce a) et) | _Emetg [emery
a 2. [Ak )=Ce-D)IETP) | 6 Empry
ie 3. [ eb) (ed) (E+ AC A
a G_ |B) - Ce-S) 1 CeREY | C# A
a ts y= (e-oy]Cetr) | ox Ab
a & | -teayetes?y 0) —_
c ¥ (CD vICere) =—
~ ERS, SEES) -¢ ABe
Ts a5 tet a
5 10 | D)/ Cetr) Zee ot
[ W DMCe+t) ae ED
2 ou
@ scanned with OKEN Scanner: T
Worle a short net on tail yecuts!
tall tecurifon be yveFen, at
on?
the last ine :
-Tain recurtion con.
aecurfon call bo
“gem ent pe
- This
moihin, smal
call Harsh
~ Pecarcive
@ scanned with OKEN ScannerWhok te the queve
Aspectoh point of et Where items are insested os one |
| end.)
end the yate end and deae From the other end(he xo
tr
~ Queuete Working Piabt in Fri} ouk CRE Fo)
arate an
Fron® Reas
IdUorioUs Features of operating 8yetem ove imple mented
Oo
using a queue:
J. Bheduling of proceseos: Ctounded vobro
T algorithm ) |
5 ;
de Spoling Cho maintain a GUEUS Job to be
prented) P
B.A queue oF client procese walitng te vecieud
service From the server, process -
2 Various application Gor twang ustag non-lineoe date,
1
[great Fite traveller
Glvucture Tee oF graph req ved ave Queue af
“Simulatlon ofa real HPe problem with the purpose oF
|
understanding dis behaviour. - |
@ scanned with OKEN ScannerT Jomriival rake
gg Bevurce time
=<
%. No oF Service counter
@ | Nome types of queue explain any one 1 deloi]
of
0.) Caplan is Implemenialion using any one methog~
Ty pes of queues _ :
LSimple queue: Pireoerquenet theala
2. plovity queue
3- double ended queue
oR.
L A-erseq Tineat queue and Circulok queue
an avsay sepresentabion of queue Yequireds 3
entitia,
a+ An ory johold queue element
6A A variable to hold the Index oF the
Front element -
© P variable 40 hold index of the tater
| element .
. # define max 30
typede? Struck quest
| jot dota Prox]5
[| int Front, tea% 5
| J queue y
| —
Se eee
ir 4
@ scanned with OKEN ScannerFront
hee veav= -1
Front = -1 .
Max= 6
—fi queue con be sepresented bya linked etruchise of
elements each element ie ctored ina node memory
aro. fot a node conbe aquired during guntme thro)
the cFunctlon © mattoe™
|
‘ + |
Front TOE |
clinked sepresentasion of aqueucs, |
4
A gueue can be declared a8 Follouis 5 - |
typeder ehruct node |
£ [
int datas ' i
Struct Node ¥ news to
‘nodes ; i
y pedet Struck @
@ scanned with OKEN Scanner—vwhere data- slore queue element
next — store the address oF next node
holding the next element of O Quey
giruchire Queue CO) has two Feld
€
R- Addresor the vare node fea
F ~ Addvecs of the Front node
ij {-$—
‘ § it
oF as We
— a : { =
A Front roo
~—t Fre .
C estolus of the Queue atte: further. ingerclion
of 9s
°
Sepplain the gueve implementatoo using any one
g e
method
5 F F
fA oat seer operation on a queue includes
4) Eni ializec ) ~ Tnitialiae. a queue by gitting —
yo __the value of sear and pront
yws0 2-)
@ scanned with OKEN ScannerInsert and dement at the wore end
Lo a oF the queue
syd queued < delebe the Front element and wvitlen
thesame-
aT ‘itken
|__| |S emply ed & Tb elev Prue C1) The queue.létmpk
and yeturas alse Co) if thequeue s
a Dot empiy. . t
7
| | 5) Pull CD 8- rb veh written Tue i) iF the queue |
is Full ond wiithen false co) iF the
queue fs not rau.
6] print 5 printing of o Queue -
Wrile a ¢ theert FINE 4wo imple ment queue
using a array ,
|= function enqueue C) =
=< Blgorithm. for ingearchen ina queue [
4 Tseng. in.on empty queweg- -
= R=
“data Tel: X53 KX isthe element to be insext
both Rand F will point to onl
yslement oF
the queue) @ 10
SS
inserting | in non ae queues:
“ke Ral 3
data fey (oy
@ scanned with OKEN ScannerJWrieashort Note on circulot Queue of mame Hi
OF queues enplain ony one in derail:
Etheve t one polenttal problem with Tmplemenlaigs
GF O queue using Simple array fhe queue mo
beappear tobe Full a although there may. be som,
Hest fn he queue a
to 2 OW ge index
_ £
pig! Z |
Hs 4 [
Front vey i
OA»? 3 4 <—index
Front reat,
-In Pause queue (6 Pall alls ht 7 |
Go U9 Seahton Cand Loe
- APter the insearton of Sel
Shown 10 HS agp
ement in the array as
and Feo - i
Qik Full
F APtet the jwo euccesalue, deletion 2 eeu $F
‘
el,
@ scanned with OKEN ScannerSauannE eens
~ Queue in FF |
gure ig full these is no empty Gpace a
head of va%e. a
Oy
~The simple solution 16 thar whenever, rave gats to |
the end of the orsay 8 atound to the begeining
TOW the, aTea oY asray cap through of a8 acivcle VU)
the first position Follows the, loot element |
oy
\
POS Sears
2\" .
eCisculay artae
-In acivculay avray ao Queue ‘is Found some were
around the circle in consilatve positions -
gont
4 { co \\'
/ asi
SSO
oe 13 7] Gonce ptiol ure
te
i ‘ i
ee
@ scanned with OKEN ScannerF Petorily queue ® an orde Niet oF homog eneous
| clemeht ina normal quede JBeTUICe 18 provi
ding first In Firsk out -
o
Fn Q priority queue gesvice (s not Provide
in a basic of first come Firs! SeTVICe but raul
that priowly base urgency of need’
~An element with Figh ee prouby proceed befote
with the lower priority .
- Elements with gome prlovtty ave
: OCR e e
*Fiveh come First gevice? a meted ox
{
De
rina que A
f fiq of 0 mie
| : LeU
| l Cicue }
@ scanned with OKEN Scanner[slack 1s Fisst in lagt uk Queue \sthe First ouk
land last in five? our which | je. b7s} in fitet ow
lis enter last will be velvived | whichis entey hive
[fiver wil be setoice Rivet
|
rStack 1% lineay dalogixuctuse | Quaelscalingay data
which ‘ig follow lagtin fivsh sisucluyeé Which +8
out « fisshin First out-
FGlackis suitable: fox reciessiue = Queue canbe used fox
fund? non Tecuresi ve Fun
° [_ \ike BFS
|
[Insertion ond ddeHonore possible - Tngeattion and aie al
through one end and Stock- #hevore and ddelionat
[the frors end in a quale -
T
LN] [
for’ e.g; ‘books in Vbsavy = teg-Ginema ticket counter
EB v | 07 OUS .FickeF counter-
ae
@ scanned with OKEN Scanner