c0870data structure
TRANSCRIPT
![Page 1: c0870Data Structure](https://reader030.vdocument.in/reader030/viewer/2022021219/56d6bfbf1a28ab30169786d9/html5/thumbnails/1.jpg)
7/25/2019 c0870Data Structure
http://slidepdf.com/reader/full/c0870data-structure 1/6
c
Programming in C : UAA 105 : L .Notes 10 : M S Prasad ( AISST)Data Strctre In C
Linked List
T!e arra" im#$ementation !as one serios dra%&ac': %e mst 'no% t!e maimm nm&er o
items %!en %e create it. T!is #resents #ro&$ems in #rograms in %!ic! t!is maimm nm&ercannot &e #redicted accrate$" %!en t!e #rogram starts #. *or t!is %e can se a data strctre
ca$$ed a linked list to o+ercome t!is $imitation.
Linked lists
T!e $in'ed $ist is a +er" $ei&$e dynamic data structure: %!ere items ma" &e added to it orde$eted rom it at %i$$ %it!ot etra o+er!eads. A #rogrammer need not %orr" a&ot !o% man"
items a #rogram %i$$ !a+e to accommodate: t!is a$$o%s s to %rite ro&st #rograms %!ic!
re,ire mc! $ess maintenance. A +er" common sorce o #ro&$ems in #rogram maintenance is
t!e need to increase t!e ca#acit" o a #rogram to !and$e $arger co$$ections: e+en t!e mostgeneros a$$o%ance or gro%t! tends to #ro+e inade,ate o+er time-
In a $in'ed $ist eac! item is a$$ocated s#ace as it is added to t!e $ist. A $in' is 'e#t %it! eac! item
to t!e net item in t!e $ist.
/ac! node o t!e $ist !as t%o e$ements
1. t!e item &eing stored in t!e $istand
. a #ointer to t!e net item in t!e
$ist
T!e $ast node in t!e $ist contains a NULL #ointer to indicate t!at it is t!e end or tail
o t!e $ist.
As items are added to a $ist memor" or a node is d"namica$$" a$$ocated. T!s t!e nm&er oitems t!at ma" &e added to a $ist is $imited on$" &" t!e amont o memor" a+ai$a&$e.
Variable for the listT!e +aria&$e %!ic! re#resents t!e $ist is sim#$" a #ointer to t!e node at t!e head o t!e $ist.
Adding to a listT!e sim#$est strateg" or adding an item to a $ist is to:
a. a$$ocate s#ace or a ne% node
![Page 2: c0870Data Structure](https://reader030.vdocument.in/reader030/viewer/2022021219/56d6bfbf1a28ab30169786d9/html5/thumbnails/2.jpg)
7/25/2019 c0870Data Structure
http://slidepdf.com/reader/full/c0870data-structure 2/6
&. co#" t!e item into it
c. ma'e t!e ne% nodes next #ointer #oint to t!e crrent !ead o t!e $ist and
d. ma'e t!e !ead o t!e $ist #oint to t!e ne%$" a$$ocated node.
T!is strateg" is ast and eicient &t eac! item is added to t!e !ead o t!e $ist.
An a$ternati+e is to create a strctre or t!e $ist %!ic! contains &ot! !ead and tai$ #ointers:
struct fifo_list {
struct node *head;
struct node *tail;};
Points to note:
a. T!is im#$ementation o or co$$ection can &e s&stitted or t!e irst one %it! no c!angesto a c$ients #rogram. 2it! t!e ece#tion o t!e added $ei&i$it" t!at an" nm&er o items
ma" &e added to or co$$ection t!is im#$ementation #ro+ides eact$" t!e same !ig! $e+e$
&e!a+ior as t!e #re+ios one.
&. T!e $in'ed $ist im#$ementation !as ec!anged $ei&i$it" or eicienc" 3 on most s"stemst!e s"stem ca$$ to a$$ocate memor" is re$ati+e$" e#ensi+e. Pre3a$$ocation in t!e arra"3
&ased im#$ementation is genera$$" more eicient. More eam#$es o sc! trade3os %i$$
&e ond $ater.
Types of List
Circularly Linked Lists4" ensring t!at t!e tai$ o t!e $ist is a$%a"s #ointing to t!e !ead %e can &i$d a circ$ar$" $in'ed$ist. I t!e eterna$ #ointer (t!e one in struct t_node in or im#$ementation) #oints to t!e
crrent tai$ o t!e $ist t!en t!e !ead is ond tri+ia$$" +ia tail->next #ermitting s to !a+eeit!er LI*6 or *I*6 $ists %it! on$" one eterna$ #ointer. In modern #rocessors t!e e% &"tes omemor" sa+ed in t!is %a" %o$d #ro&a&$" not &e regarded as signiicant. A circ$ar$" $in'ed $ist
%o$d more $i'e$" &e sed in an a##$ication %!ic! re,ired rond3ro&in sc!ed$ing or
#rocessing.
Doubly Linked Lists
Do&$" $in'ed $ists!a+e a #ointer to t!e
#receding item as %e$$as one to t!e net.
T!e" #ermit scanning or searc!ing o t!e $ist in &ot! directions. (To go &ac'%ards in a sim#$e$ist it is necessar" to go &ac' to t!e start and scan or%ards.) Man" a##$ications re,ire
searc!ing &ac'%ards and or%ards t!rog! sections o a $ist: or eam#$e searc!ing or a
common name $i'e Sing! in a te$e#!one director" %o$d #ro&a&$" need mc! scanning
![Page 3: c0870Data Structure](https://reader030.vdocument.in/reader030/viewer/2022021219/56d6bfbf1a28ab30169786d9/html5/thumbnails/3.jpg)
7/25/2019 c0870Data Structure
http://slidepdf.com/reader/full/c0870data-structure 3/6
&ac'%ards and or%ards t!rog! a sma$$ region o t!e %!o$e $ist so t!e &ac'%ard $in's &ecome
+er" se$. In t!is case t!e node strctre is a$tered to !a+e t%o $in's:struct t_node { void *item;
struct t_node *previous; struct t_node *next;
} node;
Stac' Data Strctre
T!e stac' is s LI*6 a&stract $i'e data strctre its &asica$$" associated %it! nctions P6P and
PUS7..
Ps! :3 It sim#$" #s! t!e data in#ted on t!e to# o t!e stac' $i'e i t!e stac' contains t!e
o$$o%ing data :3
819
8119
8009
Ater a Ps! ca$$ %it! as a in#t T!e stac' %i$$ c!ange to :3
89
819
8119
8009
P6P :3 Po# nction needs no in#t and sim#$" remo+es t!e +er" irst e$ement on t!e to# o t!e
stac'. I t!e stac' contains o$$o%ing &eore a Po#() ca$$ :3
89
89
8;;9
T!en ater t!e Po#() ca$$
89
8;;9
T!s a stac' can &e treated as a set o #$ates one a&o+e t!e ot!er or a #i$e o 7ea+" &oo's one
o+er ot!er...I %e need to #o# t!e rd e$ement o t!e stac' %e %o$d !a+e to #o# times and
t!en #s! t!e to# e$ements again...T!is is a disad+antage o stac'...
<=
>inc$de?stdio.!@
>inc$de?std$i&.!@ == or ma$$oc
t"#ede strct stac' =< t!e strctre is a t"#e deined as Stac' . t"#e de $ets s deine or
o%n data t"#e <=
8
![Page 4: c0870Data Structure](https://reader030.vdocument.in/reader030/viewer/2022021219/56d6bfbf1a28ab30169786d9/html5/thumbnails/4.jpg)
7/25/2019 c0870Data Structure
http://slidepdf.com/reader/full/c0870data-structure 4/6
int inoB
strct stac' < netB
9 STACB
STAC <to# NULLB == initia$ising t!e Stac' to# #ointer
+oid init()
==Print t!e men consisting o dierent o#tions (4asic nctiona$it")
+oid #rintMen()
8
#rint(EnEt1.Ps!EnEt.Po#EnEt.Dis#$a"EnEt;.FitEn)B
9
== Ps! t!e data onto t!e stac'...
+oid #s!(int data)
8
STAC <ne%B
ne% (STAC <)ma$$oc(siGeo(STAC))B
ne%3@ino dataB
ne%3@net to#B
to# ne%B
9
== Hemo+e t!e to# data rom t!e stac' and a$so dis#$a" t!e data de$eted ...
+oid #o#()
8
STAC <tm#B
i (to# NULL)
8
#rint(Stac' is em#t"--En)B
9
e$se
8 tm# to#B
#rint(Po##ed data : dEntm#3@ino)B
to# to#3@netB
ree(tm#)B
9
![Page 5: c0870Data Structure](https://reader030.vdocument.in/reader030/viewer/2022021219/56d6bfbf1a28ab30169786d9/html5/thumbnails/5.jpg)
7/25/2019 c0870Data Structure
http://slidepdf.com/reader/full/c0870data-structure 5/6
9
== Dis#$a" t!e stac'
+oid dis#$a"Stac'()
8
STAC <#trB
i(to# NULL)
8
#rint(/m#t" Stac' ---En)B
9
e$se
e$se
8
#trto#B
#rint(Stac' :3 En)B
%!i$e(#tr - NULL)
8
#rint(En En)B
#rint(J;dJ#tr3@ino)B
#tr #tr3@netB
9
9
9
=< T!e main rotine
int main()
8
int c!oice 0B
int data 0B
init()B
%!i$e(c!oice ? ;) = < %!i$e t!e c!oce is $ess t!at ; 'ee# $oo#ing
8
#rintMen()B
#rint(P$ease /nter C!oice no : )B
scan(dKc!oice)B
i(c!oice 1) =< Ps! t!e in#ted data i t!e c!oice is 1
![Page 6: c0870Data Structure](https://reader030.vdocument.in/reader030/viewer/2022021219/56d6bfbf1a28ab30169786d9/html5/thumbnails/6.jpg)
7/25/2019 c0870Data Structure
http://slidepdf.com/reader/full/c0870data-structure 6/6
8
#rint(/nter data to &e #s!ed (03) : )B
scan(dKdata)B
#s!(data)B
9
e$se i(c!oice )=< #o# t!e data is t!e c!oice is
8
#o#()B
9
e$se i(c!oice ) == dis#$a" t!e stac' i t!e c!oice is
8
dis#$a"Stac'()B
9
9
retrn(0)B
9