0 ratings 0% found this document useful (0 votes) 195 views 13 pages Data Structure in C
The document discusses data structures including stacks, queues, and linked lists, detailing their operations such as push and pop for stacks, enqueue and dequeue for queues, and the characteristics of linked lists. It explains the differences between arrays and linked lists, emphasizing memory allocation and structure. Additionally, it covers tree and graph structures, highlighting their unique properties and traversal methods.
AI-enhanced title and description
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
Go to previous items Go to next items
Save Data structure in c For Later {latte a _Progsam to read f vite one Stacks (Date [Stacks (Dake Structure)|
Dimentlonar Array. oo sory tnpur
Welude € sbsO.h>— PURE Pee oot [ft Stack Is a\ Alon - Primitive Unear dara
Include < toniOh>—_, ton cote Iapul out structure:
Cire (70, gateht.) J" Tt 1s an Ordered st In Which addition of
vols main) ne data item and delerion of alriady existing
4 Jot a [iol, i data item fs dong from onty one End
Known qs Tép of stack (Tos )
ars(re)3
Print F (“enter tr Array elements”) 5 ee — Pusy
for Uno; , 1<=43 5 Set) s
t u yes
Sean€ ("“44", faltl); 2 anal
> £
Print ¢ (“the entered Arvay 15")5 '
for (i=0; ie; f44) A
§ one e (“ra\n", alt);
Je The tact qaded element whi be the Flyst to
be Removed From the stank.
“Tris Is phe reason chaux Is Called
Cast-}n-pirst-out (LIFO) type oF sfstSe ran F Agen
© are twe Operation oF stauc. Statk has two Operation
4): Push operation,
i) +The process oF adding |). PoP operation
4 mew element to the 40P oF Stack Ts called
2. [PosH Oerarlion Lethe procest af adding 4
+ Every nem element fs adding fo stax 4op | new element of the top of stout Ts catled
is incremented by one ust Operatton
+ Th case ty arroy Ys Fan gnd no new element Jo Every Pusy operation Top is lncremented
can be qaded it's called Stack full ox by one
Stack OverFiew condition.
2)-[POP Operation} + Tia process of avieting an
element From the top oF StQUK (alitd ° In ease th Array is Fun no net element
PoP oferation . ts added - this condifon js called
+ After every PoP operation the stack (Top? is Stack Fun of Stace OverFied lonaiHon.
dewremented by One.
+ Th there fs no element on tv ctauk and pu [4F Algorithm for Tncertng an item Into
PoP is pesformed thin tes in vesuit Into prt Shou (pos Operation).
Stack underA0® Conairion. Fe ral
Tee=stoe+s |SWEVEVEE EEL b eee ddd dd ddd ddddd dll
Posy ( Stack Umax size), tem)
Step 1: initiatize Stax (por)
gut tepe-t PoP ( stack Emax Size, trem)
Step 22 Repeat Steps S40 S until Topcmaxsize-!| Step: Repeat steps 2404 until Top 20
Steps: Read Item Step 2: Set trem = Stax CTP]
Stepus Set top = foe +) Steps: Ser fop = top-!
Step S: Set stausCtop] =item Step: Print, M0. deleted is, [tern
Step 6: Print " Stank overflow” Step $: Print stank under Flos
2. [For Opeationy]
+The process oF deleting an element From
tht fop of stack is called POP Operation.
* Alter every POF Operation tw Stack TOP
is clecrementtd by one
ToP = Top -1
o IF there ts no element on th fra and thy
Po? operation ic performed prin cris whi result]
jnto STACK UNDERFLOW. condition
‘Por openanien) + POP
Fodelered [ze] Tor =o.
a 10
* Algorithm for delethng on item from tre
Stacks (Prefix gf Post Fix)
D.[InFigWotatlon] PUheve tne opecaror ts
written Tn baween Che Operands.
Ex ATE +t Operator A,B operands:
oy [PreFix notarlon] In mis operator ts wytten
before pr Operands -
Tris alse Knoon as Folich nlotatton
ex +Aag
8) [fostetx oration] Th ants operator Ve uasttten
aFrer the operands.
Tr fs aise Knoton as Surrix Notation,
ext ARF
rem
eeeQ> Convert the Porlordtng Inthe to Prefix and
Postel For (A+B) * C/D +E F/G
Prefix @ (A+B) * c/D + EN FIG
+ ABR CD FENG
Let As =R
Rik c[D + E*FIG
R* fp AEFq
£2 NEF =o
Re YD + RG
Ry K 1D + Raf
Leased =Rx_
Rix Re hala
QR & Ry +1 RG
ta {RG = Py
Rho + Ru
K RAR: Ry
Now wen Rs bu, Rx Ra, Ry
Postftx > (a+B) « 0 + E7F/q
(B+) & clo + EF]
ket ABE
Rik oD 4 EC F/¢
erates al
Let EPP
Qk C0 + BIG
Q, * GOD) + Raq
eet coy = Rs
RK Ret Ral
Ri RRs +@4))
Let R2Gs «Ry
Q¥ ke thu
Gita +84
Cet Roe = Rs
Rs +Ry
Rs Rut
Noto enter tre value oF Re, Rahs, Ra,
Re tut
RR eRe +
Age coP™ goat
AB cos x Er dq+
Ue pe stklx epressionrePlx and fostFix using tabular form fabular form
convert CA+B KC) Into prefix and postétypsumbel Scanned
using pobwar form
4+ 40 Convert tn Preflx Fonowlng operation
Program
Reverse re fnput sting
Ferform tabuiay mutnod and Find postelx
exprecclon So tna postAx Exprection c@ KAT. Now
pee ante postéix Expression string 4o reverse thls Expression to ger the prefix
Sind the Prefix. ar
6222S 0 pretix 16 FAK BC Greene
First 40. add branches He Convert post ety 9 Diveur perform rabuiar
C* cB
ce CBR
ct BRA
Shy che AT
voto an
(arexe) faim (A+B #C) :
Reverse string ane Srack] [ Poartlx Expre:
(e«s+a) q
Prioviny By
A.7 highest age
17 2 highest AGCEt
$a Lt erty,Insert 19 geo gy reo
[roy
* Queue ts nlon- Prtmitive Unar date shuure. worrs
+ Tk 1s an homogeneous coutetton of elements rh
in With new erements are qaded at one End
canted th Rear Ends and tre existing fee ere
element ave derettd from other End Caled Fes
tha Eront end eR
+ The First added element whl be th Flyst 40
be remove from th queue. thor ts the Inset %0 Ror F FO
yeason queue Vs coves (ere0) Fixcr-)n - [te [20] 30 |
Ik Hey
che
Pirst our type itt. 5 ;
+ Th qptue every Insert opiratton gear ts aac
incrementtd by one daer element. First dewre to
R=Be1 R=2 £ Fa)
and every delered operation Fyont 1+ mC
incremented by one t x
Feet!
* eel ent dolere Second element
Gr Pe EER bs Roa f Fer
noEG CIB
gene ewe fhSVU vebeebbesedd ids sid dd ddd der
Operation on Queue
S| py [te aur an element from env awe]
Algo: — Qinseer [@veve [moxstze), trem )
Step 1:
Qperete ( Quevelmaxsizel, }
TrtHatization
Set Front
cet Reavy =~!
Step2: Repeor steps 3105 unt]
Reavy < Maxcize
Step 3: Read Item
Seep ut If Front
Step 1: Repeat step 2+0 4 unt)
fronr 7 =0
Step2: Sut trem = Queue Front]
Step3: Te front = = Rear
set Front =-)
set Rea
else
front = Front +)
Frint, alo. Derered Ys) tren
> Friar "Queue Fs Empry or
_UnderFlow
she
Srp
Rear = Rear +!
Srp 6: Ser Queve LReqy] = trem
Frint , Queue is OverFiowdNMWWVELEE ELEC eC dd dd dtd dd ddd di irwur
3). Each Hme a new clomenr Is Inserted tnto
tha queue the Rear Is Incremented by one
ae A Clvcway queue Ts one to wanlch tre Rear = Rear +)
1 Hon OF a new element ts dont af the very fu): Earn tme an element is depeted from 4H
Arrst locarton of the queue ff the lait jocatfon queue tru value of Front ts Ineremented by ant
of queue is Fun ond Sm Front = Front +1
Feee-t
43 Algo % CINSERT DE Lmoaxsize], [tem )
os %ay ‘Srop 3 9 1F(Front= =(Kear+i)¥- maxsize )
Onlit queue 1s over Flow £ Ext
Else: falet the value
te ( Front = =-1)
Ser Fronr =0
Rear =©
Circular queue hag following tondfHont Else
Raar=(CRear +1) 7- maxsize)
[Assign vauel Queue Reav) = votue
fend 6)
4 A Grewar queue overcome tru Problem of
tunuHlitzed space In Linar queues }mpremenred
ag arrays
1). Front will autays be pointing fo mu First
element
2): TF Front = Reav tht queue why be empry-
2Queve (para Structure)
OPeration on Cueue
exe
1). Front
10,20, 30, 40
Praxsize = 3
> empry queue
Rear =
31S Shep Repeat
R< maxstze -1
<3
oe ae
fs
24 ‘5
Read then
Read 19
ne
tance D104)
ser y Lo]
Ylo} =10
quae Gel _[
vol 00 90)
R=0
Rear < maasize -|
o<3-
O< ate
Read 20
te fas
O= = -2F ate
cise.
Reka
Reot)
Rey
Yon = 20
[jelae[
Vis $0) yt)
Fee, R21
2) Rear < mowsize -)
beset
1 <2 true
Read ro
Teed
O==-1 False
Else
POO OEE —— eS
ttem
[el_T]
qi V0 4f2]
(ie) 35 [3°]
CoRonTs)
Feo, Ren
Jeasey Rear < maxsize -1
2<3-1
2<2 ease
© Yurue fs overFiowwi bbe bbe besebecdcdcddddidddddddlidiurY
Derre an Element in Clyeway Vue
ence: Front = (Ceront +1) 1: maxsize)
ext
Qvevere ( Queue [maxsize], ftem)
i€ (front =
1
WII queue underfiew ang Extt
Trm = Queue [Front]
t¢ (Fron
set Front =-1
set Rear > -1
Rear)
Cena 1 crarement )
— ftem deleted
2). ertr
ic
Structure )
Duete operation On Gutue
[re [ze
30
Med 96) $2)
Red
Feo
praxstee =3
f>20
O>=° true
set Neem = q (0)
trem =10
2 False
Feet
f2 O41
4). Irem ts guued
10 ty dared
Ye) 41) 909
Pet Ron
mace
Fel Q22WVVVe eke bed dddddbdddddddddl/l/
Fret
€ atti =2
y Trem is deures
a0 $4 daured
Fez
case 3).
yD F>=0
a>rorme
2 trem = | (2)
them = 70
Ie Fo aR
2 2 2 fue
Y Yem Vs dered
(aT ae anal
Fe-t
Re-4
Casey)
>=
F
ler
5! Yuewe te empry
fs Minder Fiow.
A Naked Ast js a Mineay clara struurare
Th nich gre elements are not stored at
tonriquous memory Lotatfon
A Minled Aiur Isa dynamic dara
Trt no. of nodes tn not Fixed
and «an grow and shyink on demand
strucrare
abst Fo
Each Element fs coed @ node Nth has
two parts.
Info part wrth stores gre |nfermarion
ang falar, a point #0 tRiviey een
exy [le [i234]
treo Potarr
| foseo)panr fnreQ)
[}~fSQLVUC UEC TE CTT TIT I TIT II I deed fh
DIFFERENCE BETSEEN ARRAY AND LINKED List ]
LINKED List
OF an Array is Fixed
fir7ay js a colleetton oF Homogenccus
Cstmtlar) are type
Memory 15
anjecared From stauc
Frio Work With stark dara
Struc re
Elemente ase
memory tocatio
jored In Conttguous
Frevoy elements axe fndependent to
each opner-
Prvey tele more time
Cinsertion gy’ peterfon)
frac eomne Cumnen vs),
> Memory 1
Size of + is not Fixed
Moked ust ts a Collection of node
(dara y addvess)
$ aljocares From heap
Linked ist work wlrn aynamte garg
Ctructure
«Elements Can be sored anywhere
tn the memory.
Liniad ist elemenre are depend 40
each opher:
Mnked - bet gare less times
C Insertion g petetfon’)@ Cotleuion of nodet and Edges
= {node , edges}
Thiet fe @ unique node cated yoot in tree
- There tolll not be any cycie/ Loops
Represents dara tn pre form of a tree
Struurure, In @ hlevarentea) manner.
Th tree only one path bertoeen pO
nedes
In tus preorder, In ovderand preorder
Traversal
ent
Ara eomng ChLieticepiat).
4). Gyoph ts @ coneuton OF vertces| nodes
and Edges:
exe q=4v,ey
2): There 1s no unique node-
B)- There can be loops /cycie.
U): Represents dara stenfiay to 4
nerwork.
5)-In GyaPh One ox more then one path
berween two noses
In tric Ges and DFS traversal.