0% found this document useful (0 votes)
6 views22 pages

Data Structures: Linked Lists Explained

The document discusses various data structures, including linked lists, stacks, and trees, highlighting their types and implementations. It explains the differences between static and dynamic data structures, as well as self-referential structures. Additionally, it covers memory allocation techniques and provides code examples for linked list operations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views22 pages

Data Structures: Linked Lists Explained

The document discusses various data structures, including linked lists, stacks, and trees, highlighting their types and implementations. It explains the differences between static and dynamic data structures, as well as self-referential structures. Additionally, it covers memory allocation techniques and provides code examples for linked list operations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

kS0eESH KuMAR

UNT-I IST
[Link] Mernory
Sll-Rafeential implementation - singly linted
ALLoxation. linked list ciuas linked list Applato
Doubly linked ist,
list.
ot i&t

what is Date stiuctuse stosage that s kuied


*AData stiuctuie s a
way cf aiaging
Oxganize [Link] sa
to stoe and be aceSSed anel
sothat is Can
a Compte
data on 6G

lepdated etticietly used tor OsGanizing


stuctue not ony
*A data
it also esed fos
Data,
’ paccesi ng
'Rctieving
’storing Dala
classfication of Datd stnctele:
LData stuctuie
INonLnca

incas Daa LData stxectuie


strectwYe

Dynanic Tee |Gpaph


Static suctue
Dala
Data stuctud

Ceue
stack|linkallist
Anay
lincas Data stuctuie:
stiuctee Lo which Data elements ale
*Data each
"lincaaly " whece
aianged sequentially or its"previous" and "nert"
elenents s atlahed to Detta steetues
elements Caled lincas
adjaccnt
EXamplzs
* ucue
7 Stack
* linked List
structuse:
Types of Aincat Data Data structie
* Static
Dynamic Data stuctuie
Dynamic Data structte
Static Data
shuctuc not fixodt
The si ze s
It Ls a fixed
memoY
It Can be sandomy
IHæealies to updated duing the
in eotina which may be
aces the elements
Dota stucteie Cons deied e t f i i i n t
la static
tho memo y
Coneeing
(Spae) (omplexity of
the Codo.
EY: stack, Qeue...
etc.
Ex: Anay
Non-lincaa Data structue:
cláta elements aie
Data stuctue whec called
ox lincaally aie
hot placed sequentially
non_inaas clata stuctuues element
jiavese all the
dont
in n-l-s.(we
in a singlo en only w m e m o x y a l l o c a t i o y

Ex: Te graphs
206
200 2o) 202 203 204 2os
UB F DATEJC|

456
2 3

index
Self- Refereoital stuctues
stauctuses ac thee
Refeeoital which
*Sei<- or mere
pointes
one
stuctes
that have resttie, as theu nember
same type of
peiot to thestruct nade f
jot data ;
chai data 2;
suct node link:

words, shuctues poioting the


"in othe sely iafeenital in
stractuies ae
same tye ot
natuee".
sely Reterenital struct wtes:
Types Ot single liak
Reterenital stucties with
I Sell wth Meltiplo link
suctuses
2. sely Releienitad
S.R Stuctusa with sinale link only cne sell pontee
’ These stuctuas Can hae
as theis numbei

20 30 40 X
ob
Ex:
# inlude <stdio.h>
shct noce f
int datal;
cha cata2;
Stuct nodo* link;

int maio()

struct nado cb ; l/Ndo


1
/initialization

Oba. lino = NUL:


[Link] 10;
ob2: data o
/Inihializtio
Nult;
oba .iok = piott(/d'. cbi . lnds
oba. ddata1 -30; priotf (n/d [Link]
ob .data 24o;
J link ob)p ob2
obt. lok -pob2
of cba with ob)
IACess data member
S.R stuctuies with Meltiplo ink: with mattiple
* Sel< Re<eenital stiuctes
one sel<-poiotes
links an have more than
Complicted date steuctukes Cao be
mangy stuctses'. Buch
lasily Constiucted asing
thee
to moo than onc hode
stctúes Can easily Connect
at atime.
Ex:
Obs Oba ob3

Reters proqram ’ Gyeks for yjseks


Apliatins
* linked ists
* Stack
Qeue
Tree

Rafex ppr in laptop


AlLocATION :
DyNAMIc MEMORY
Basic idoa situations wheie Dala
’Many time Ne tae
QUe Dynamíc io natue
predicted"
"Ameunt of Data Cannot be
Data tems keeps changing Deing
Number ot
Program execution"

Such asituation Can be hdndle mora


Dgnamic Memong location:
kequrod Can be speitied at
Spae
*Memoryexecution.
the time of
supposta allocating and treezing memoxy
xoutines
dyamially Lsing libay
Memorg Allacation poess
inc
Aocal variables stack
Free memor Heap
Global Vaiables permaneot storage
aree
Inshuctions

permanent storge Aea:


and the global Vanab les
* Ihe progrrom instnctions
peimanent storage
ae storeed io a region Known as
Aiea.
stack:
The loCal Variables cse stored in anothee lea
Called"stack"

Heap:
* The mem0sy spao between the two acas
availasle for dynamic allo cotion duning the
eXeutioo of the piogram.
-This free kegion s Called the "Heap'
- The siza of the hcaps kaeps changing
Alloation tunction:
Menory
- Mallot : nmber of bytes and Retuna
Alloates Resuosted
byte of the allocted spaue.
pointoss to the fist
- Calloc: ailay of elements,
Allocates &paee foy an to
and the setuns a pointa Io
itialize them t Zeio
the menorg
- Fse:(memorydeatocation)
Frees prcviously alloated spaces
- Yedloc
size of peviously allocated spae
Modifies the
Allocating a Block of memoy : allocated LSing
* A Block of mermoy Can be
the tunction malloc of speciticd
Resewes a block of memoy
- "void'
pointea of ty
Size and retums a pointet Cao be bo ty pe - Caited to.
-The yetun
any pointe type
Gfeneral tomat: byte size);
ptr - (type ) malloc (
Ex:
malloc (10o* size of (int ));
Þ =(int)
Contine to Reter PpT
linked list implomentaton:
Linted list:
data [Link] which
It a lincas Contiguous locations,
elements aio not stored
at aa
aie linkeod csing pointes Connectad nades
Yathes they a setes of
forms of
* linked ist tho data and the addyess
stores
Whee eochnoce
the noxt node

noxt
Pdata rext}
dta next}NOLL
HEAD data notdata
Node
Node
stractue: typically Consits
xAnodeio a linked list
of two Components: the actual yeue or data
Data:It holdts
associatedwith the node.
memory a ddress (retoena)
It atores the
Noxt pointer: hode n the Beauonee
of the next

Abstact Data Type


Defnition:
is defincd eotiely by
* ADatatg thatares to as abstact
Li eg
the set of opeiations
oy simply ADT modular desigb,
* ADT is an extension cf
of
operations sch as ypion,intesectio
An ADT iaset of
Complemcot, ctc.
Jist Abstct Data Type: elomonts. The geneial
list & an Ordécd of
form of the list i.
A, A, A3,.... An
lst
Aj- Fist elemeot of the
ist
Ao dast eleneot ef the
NSize of tho I:st
Various opeations peformad on link: aBter the position
-Insert (X,5)-insert the elemeot X
Delete(x) - The element x& deletedl
2.
3. tind &) Retun the position of xs succos
suCCesSor
position of
4 Next i) - Retun the
elemest (i+1) its predelessor
position ot the
Retunthe
6 previousi) - (i-)
list s displayd.
list - Contentsof the
6b piot list empBy
Makes the
7. Maka empty - ADT
Implemoots of list
1 Aiay Iplamentation
2 lintad ist implemontaion
3 Cutsar implementtion
Inplemeotation of ist:
Aray bo
4o 50
20 |030 of datatype
ctionof spec'fic no.
is a colle storing and
AnayConnectivity memovg lo
cation in
Storage in more data elemeot . Find and prnt
deleting operations ine
list ojeations takes Constaot
2. linked ist Lmplemeotation:
lio ked list Consis of seiÃes of node eah
node Cotains elements and pointe . The pointe of the
last nodo point to null
Data element Next pointer)
Noce
Insertion and dele fion opeiations aie
peafomed using linked list easily
Types oflinked list
|: Singly intod list
2. Doubly linkad list
3. Graelar inked list

)Singy linked lit


1o00t eo tbo0+ 30 800
350 400 J000 &00

Asingle linked list in wbicb each node Contalns


only the linked field pointing to the hort node in
thelist
Sygntay
shuct nade

iot elemet;
positim noxt ;
insert an elemeot in the list
Routine to ungue
(int x, LiA L, position
p)
Void insert
position Newnodk (stuct node) )
malloc(size of
Newnedlo -
)
1f (newnade != NUu
Newnode Element =X;
Newnade ’Next -p-’Next;

P’Next = New node

|lo o0o newnccle

20 200030 3000 4o 4o00


303000eyo0o
10 1000

hether the list & eopty


Routine lo check
iot Tsempty (List L)
i4Ldoxt Nol)
vetun ();
3
Heador
Enphy list
Routine to check whethes the Cuseoot postion is last
int staat (positian [Link] L)
if (p-next - = NUW)
xetunlr);

Find Routne:
position Find (intx, list L)

positin pi
p:lext;
While lp = NUlL ? pP’ elemcol J==X)
P=p>next;
Yetum (p );

Find preious Routine : (inty, list L)


positian tind
find presious
position p;
P:l; p’next- elemest}=
(ppnext!
=NULp
While
P= pnext);

3
Find Next Routine
(int x,list L)
position tind next

p = l - next; Elemest!= =
heet ! NULL P2 p-’
While(p’
P p ’ next;
vetun p hert;

LTH02025)

elemeot from the ist


Routine ta delete an
L)
void pelote (iot x, list

position p. Temp i
Find previos (x, L);
P:
if ( last (p. L))
Temp p ’ next
:
p’next = Temp-neat;
:

free (Temp):
Temp
25
Before deletion

Atter deletion
Routine to delet s t
Void delete list (List
L)

pos?tion p. Temp
P=l’ nexti
Whilo (p!-Nuu)

Terop -p-nert;

2Temp;

10
next = Nul

leop p’ neet
|2o/
20 ee (i)p- temp
P Teop.p: NQLL
Double inked list:
Adoubly dinked iist bich eah node
as the felel namely datafield, fowad link
bakwact link(BNK).FNk poinz
(Flik)and BlNk
Secco3SOs nade in the lst Vani ous.
to nade
poiot to the previous predesscos
DATA
|BlINkPATFLINk
TELEMEN
10

Shuctue Declaation
shuct node
int Element ;
Shuct node X FLINK;
Shuct Node * BliNk;
Doubly nked list:
3:to insert an ele meots n a
Routin 1, positon p)
Void ioseyt intx, List
shuct ode * Newnode;
Malloc (size of (stact nade)):
Newnode =
if (Neunodo ! =nult)
Newnede’Elemeot : X;
Newnode FliNk =pNk;
P’ FlINk Bli Nk Newnode;
p’ FNk = Newnodo :
New node BlNk p;

delete on elemeot
Routine to
delete (int x, List 1)
Vgid

positicn p:
p(find x, L);
it (znseut(p.L)):
Temp - P;
pBliNk’FliNk =p-’ FlINk;
BliNk;
p’ Fl1Nk ’ BlINk - p-’
free lTeog)
3
3
Ciralas linkod list of the last
liskad list the poito
In ciruas linkod list an
node . A Circeula
noda to the fist dousle
singla lin ked dist and
ba implemented as without heade.
linked lst with ox
i)singly linked list linked isk s whicb
Asingle Circulaspojnt to the s t or
last nado of the list
the
header
Heador

i) Doublo linked list: circalay ist å a ink


Acoubled lin kad
of the last node
the foywad iik
Aist in kbich backwal link of
the fist hode and
points do last nede
points do the
the tistnode
of linkad list
Application
I Polynomial ADT
2. Radix Sort
3. Mult ist

polyromial ADT: polyromi ad maripulation


pelom the
TH (an subtaction and diffesentiation.

add'tion,
Cf polynomiad
Suchas implomet ation
linked at
Declaaton for
ADT stuct poly

int Coe}f:
int poWes;
stuct poy *Next ;
li'st 1,*list2, * bst &
3* polynomials:
Addition of two
Void add )

*phT, *newnale;
poy * ptr, ;
ptrl = dist
NOLLp? ptr2!= Nul)
Whilo lpt I! =
(stuctpoly)):

Newncde = malloc(size of
phr 2 ’poken)
(phl’pOkes
iH ’
(oef =phl’ oefitph
nuwnode
neonodo ’poer =p r i p o ;
nwnodo net = NULL;
list 3 - (reate (list 3, new node );
phrl =ptrl nexti
prt2 phn’net;
else
(phri’power >phra pchec)
newnade -Coefs =phl’ Coeft ;
newnace power - pbrl power;
newncde ’neet = NULL:
noonodo
list 3 = Create (list 3, newnode );
phl pbrl ’ neat
else

heonode oeff -pt2 ’ Coet ;


neunode power - ptr2 ’ poen;
next = null,
newnede
newnccde ),
ist 3 =(seate (ist 3,
next:
phr2 - pa

3
&ubstraction:
Void seb ()

poly ptr, *phra,*newnode


pt : ist,
þt 2 : st2;
!=Noll f pphra! =NoLL)
while(pt
(stct poly J);
(size of
nwnode - malloc );
=== Ptr2 ’poue
(pB poner
iif (pbl
=pti loeptr2 -loft :
newincde -’ loeff
newnedepouer - ptri pole;
newnode ’ next - NOLl;
( Ist 3, newnede ):
list 3
3 z (reate
ptr = ptrl’ next;
p2 ’ hext;
pt 2
else
if (ptl’powei
-pta ’powen)
lof;
newnode ’ loff -pt power;
ncwnode poney -ptbl
newnodenext = null;'
3, hwnode )
(reate /list
ist 3 =
ptri =ptl’ nect

else

hewncde power =pr ’pobn ;


newnode
next = n l l ,
(reale (ist 3, new nole);
list 3 -
y noxt;
}P2 =
prz
polynoial Diffoeotiation:
Void diff()

*pt , *ncwnodo,;
poly
ptyl listi;
Wbilo (pb|| =NOLL)
newnode =nalloc (si2e of (sbuctpoly)),

neonode ’ power =ptrl ’ poes -l;


hewnodo nex tf null
list 3 - (veate /ist3, newnode );
ptl - phi ’next

3 polynomial.:
Creation tor the nluo no de i )
poly **headi, poly *
poly Crcatel

poly *ph ;
i / headi= =NOLl)
headi newnodo i;
vetuso (head1)

else
phr head1;
= NULL )
Whila( p h n e x t !
ptr ptnext;
> next =new node ;
'vetum (head)l;
Radix Sort (or) Cardsort general'zed for tte
buclet sort
’ Radix sort the sing buckets in oto q
’IH an be pexfomad the elements ase sorted
>In fst pass, allsignitiant bit
according to the
deast alanged
the nembos Ue Ce

Tn setond pass,
’in sion? ficant bit and so
the next least
aCcovding to uotilu ft aeaches the most
protess a kepeated
on this numbet.
Significant bits of all radix last sort depencls
no. of passes in a
-’ The the nembe 0f givon
the ho. of digíts in 5, 1H4 184
30, l0,8,
eg: 25, 256.
pass!:
25b)
30) 22. 3 4 5 6
14,25, I5,25b, 184,8
30, lo,
Pass 2 80

O8
2 3
146, 80, 187
08, 10,I5, 25,25b,

Pass3 HH,080o, 187


015,025.25b,
008, O10,

025

olo
2sb 9
4
008 2
Ig, 25b
b 15, 025, 080, |74
008. O10
Aftes pass : 025,080,
Hy,187,A5h

008, olo, ol5,

maltfls. Tt &
Mutilast:
link ist
*More Compliated segistratio, eploqee
(usfd to maintais studosts
etc
involved in ditfoent projects takes Weetim
spae but
* Mulilist save
to in implemented numbu of
employee Can îovolve in any
* An Can be implomeotad by
projects and cach projects Norking and
emplayee. Employec (Ei)és hoxking on
any no ot of project (p2) and Es i
E2is Norking on inplemented by the
project (p1).poject (p1)el and e3 project p2 æ
emplaya emplyee e2
implemcated by the employe
E3
E2

You might also like