0% found this document useful (0 votes)
61 views

Linked List

The document discusses linked lists, which are a data structure made up of nodes that contain data and a pointer to the next node. Each node contains a data field and a pointer to the next node. The first node will point to the next node and so on, forming a chain. This allows for efficient insertion and deletion of nodes throughout the list.

Uploaded by

mrrobot0987654
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)
61 views

Linked List

The document discusses linked lists, which are a data structure made up of nodes that contain data and a pointer to the next node. Each node contains a data field and a pointer to the next node. The first node will point to the next node and so on, forming a chain. This allows for efficient insertion and deletion of nodes throughout the list.

Uploaded by

mrrobot0987654
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
You are on page 1/ 19

LINKEDLISTI

Pooblemwitharays
. :
Fined size

¥ What is a linked list ?

linked of nodes and


ARE list is a collection where each node contain data

pointer to next node .

data NODE

FIRST


100

↳ →

210 270 300


200

Struct Node SELF REFERENTIAL STRUCTURE

{
int data ;
-
2

Struct Node
*
neut ;
-

2- ( same as datatype)

} ; 4
bytes

*
struct Node p ;

p
=
( struct Node * Imalloc ( size of ( struct Node ) ) ;

Node ; Ctt
p new
=

p
→ data =
lo , ⑤
→ next
p o
=
;

500

X -

x -

struct Node *
p,
* .

I 3 → neut
gqFP 2
q =p
→ neat p =p
-
. .

p g p g p or

④ ⑤ ④ ④ ④ ⑤
ft w I
② →

200 200 210 200 210
*
Struct Node p
=
NULL ;

if ( p

-
-
=
NULL ) To check if pointer
if ( p ) is
o not
pointing anywhere
= =

if C ! p)
if (p → neat ==
NULL)

if Lp ! =
NULL)

if (p ! o) To check if pointer
=

if ( p) is not NULL .

if (p → neut ! = NULL )

TRAVERSINGTHROUGHLINKEDl.IS#

* FIRST
Struct Node p =
first ; 200

while ( p ! o) ⑤ p=
f
=

too
{
→ neut

p =p ;
210 270 300
200
}

t# include cstdio -
h>

# include < Stlib h > -

Struct Node

{
int data ;
*
struct Node neut ;

} *
first =
NULL ; check struct Node
*
first =
NULL in main

void create Lint ACT ,


int n )

{
int i ;
* *
struct Node t ,
last ;

first =
( struct Node A) malloc ( size of ( struct Node) ) ;

first → data = Ato)

first →
neut =
NULL ;

last =

first ;
fool i =L ; ien ; itt )

{
t =
( struct node 9) malloc ( size of ( struct Node ) ) ;
t → data =
Afi) ;
t → neut =
NULL ;

last →
neut = t
;
last = t
;
}
}

Void
*

Display ( struct Node p)

{
While ( p ! =
NULL )

{
''
"
printfl f. d →
data) ;
,
p
→ neat
p =p ;
}
}

Void mains
{
*
Struct Node temp ;
int AE] =
{ 3 , 5,7 , 10,25 , 8
, 32,2} ;
create (A) 8 ) ;

Display ( first ) ;
}

RECURSIVEDISPLAYOFLINKED.LI#
d ( 200)
FIRST
/

⑤ 8 I
100 d. (210)

/
-

↳ 210

270
3
I
200 300
-
d (z> o)
>

I
*
void Display ( struct Node p)
V
STACK
{ -
d ( 300)

p%
12
if (p ! =
NULL ) p= 0 I
×
{
" "

printfl Id
, p

data) ;

( p 210
Display neut ) ;

p =

} p=
200

}
Display I first );
*
d ( 200 )
void Display ( struct Node p) )
/ ^

/
{ 8

if (p ! =
NULL ) d ( 210 )
\
{ / 3

pinning.ly?ii.npentjaia, 4h '\
"

} d ( 300)

} Ocn) I 112
×
COUNTINGNODES.IN#kEDLlST
*
int count ( struct Node p)
{
int c=o ; Oln)
while Lp ! - O)

{
Ctt ;


p =p neut
;
g
return cc) ; 4200 ) =
4

} It
RECURSNEFUNCT1ONF0RC0UNTlNhNUMBEROFNOD# C ( 210 ) + I

/ 34
*
int count ( struct Node p)
{ O (n) C ( 270) + I

if ( p = -
o ) / Zf
return 0 ; c ( 300) t l

else / T
return count Lp → neat ) -11 ; CCO ) + I

} I f
X O

SUMOFALLELEMEN-SINALINKEDl.IS#

add ( struct Node )


*
int

{ USING RECURSION

int sum = 0 ; int Add ( struct Node *


p)
while ( p ) { Oln)
{ if ( p ==o )
sum → data ; return 0 ;
sum
p
=
+

else
p =p neut

;
} return Add (p →
neut ) t → data ;
p
return sum ; }
}
MAXIMUMELEMENTINALINK.EDU#
RECURSION
int man ( struct Node Tp ) int man ( Node
*
p)
{ {
int m = -
32768 ; int n=0 ;

while ( p) if l p==o )
{ return MIN - INT ;

if Lp → data > m ) else


{
→ data ; u (p → neut )
=p man ;
=
m

p
=
p → neut ; if In >
p
→ data)

} return n
;
return (m) ; else

} }
return p
→ data;

SEARCHINGINAL.IN#EDLlST

Binary search is not suitable


for linked list as we cannot
go directly in the

middle of the list

LINEARSEARCHRECURSIVE
- -
( key ) Search ( Node
* *
Node search struct Node p ,
int Node p ,
int
key )
{ {
if ( p== NULL)
!
while ( p =
NULL) return NULL ;

{ if ( →
data)
hey =p
-

if
they =p → data) return ( p ) ;

return Cp ) ; return search ( p ' neut


hey );
-

}
p=p → neat ;
}
return NULL ;
}
IMPROVINGSEARCHINGN
ode *
search ( Node -
P ,
int
key )
{
*
Node NULL ;
q=

while l p ! = NULL )

{
if ( =p → data)
key
{
→ neut → neat ;
q =p
p

neut =
first ;
first =p ;
}

q =p ;
p =p → neut ;
} void Insert ( int pos ,
int n )
} {
* *
Node t , p ;
==0 )
INSERTINGINALINKEDl.IS# if ( pos
{

Befooefiostivode t -
new Node ;

+ →
data =n ;
*
Node t= new Node ; t → neat -
first ;
t → data =n
; first - t ;
t 3
→ neut =
first ;
first = t

else if ( pos > o )

Pos {
p= first ;
Node
ddpjitt )
*
Liao ; icpos
t new Node ;
for I
= -

+ → data =u
; p =p → neut ;

p =

first ; if ( p )

for Li - o
; is pos l ; itt )
-

{
p =p → neut ; t -_
new Node ;

+ →
neut =p
→ neut ; t → data -_ n ;

p → neut =t ; t → neat =p → neut;

p→nent=t ;
}
}
}
INSERTINGATLASTVOIDInsert last ( int n)

{
*
Node t =
new Node ;
t → data =
u ;
t → neut =
NULL ;

if I first ==
NULL)

first = last =t ;

}
else
{

last →
neat =t ;

last = t ;

}
}

INSERTINGINASORTEDl.IS#

FIRST


100

↳ 210

270
200 300

p
=
first ;
q
= NULL ( Tailing Pointer )

while (
p Ld data )

p
cu

q =p ;

p =p neat ;

}
t =
new Node ;

t → data =n ;

t → neut → neut
q
=
;
neat t ;
q→
=
DELETIONFROMl.IN#EDLIST

(t ) Deletionoffirstivode

*
Node p
=
first ; 04 )

first =
first → neut ;

me
p
→ data;

free (p);

(2) Deletionfromagivenposi.tl#

Node tip =
first ; min Oct )
*
Node NULL ; man 0 ( n)
q
=

for (i = o ; is pos -
l ; itt )
{

q =p ;
p =p → neut ;
}


neat → neut ;
q =p
n =
p
→ data ;

free (p);

(HECkIFLlSTIRTED

int u =
-
32768 ; Oln)

first ;
*
Node p =

while l ! = NULL)
p
{
if ( p →
data en )
return -
l ;
→ data
u =
p ;
→ neut
p =p ;
}
return 0 ;
REMOVEDUPLICATESFROMl.IS#

*
Node p
=
first ; Oln)
*
Node q
=
first → neut ;

while ( ! NULL)
q =

{
if I p → data ! → data)
=
q
{

p=g ;

neat ;
q= g
}
else

{

p neut =
q
→ neat ;

free Cg ) ;
→ neat
g =p ;
g
}

REVERSING
-
A LINKED LIST

Reversing links is preferred over


reversing elements

(1) Reversing because of


Elements
maybe these are lots values in a
single
(2) node
Reversing links .

(1) ReversingEleme

First A of of linked list


create an
array equal to size
length .

/
p= first ; Oln) p
=
first ; i -
-

;
i = O ;

while (p ! =
NULL )

while l p ! =
NULL) {
' is ,
data , Fiat:
I ,
itt ;

}
*

(2)
Reversinglinks TRACE IT !

p= first ;
NULL ;
q
=

y =
NULL ;
while ( ! NULL )
p
-
-

sliding pointers

)
r=q ;
q=p ;
p =p neat

;

mentor ;
q
}

first -_q ;

REVERSINhl.IN/eEDLlSTUS1NhRECURS#

Void Reverse ( Node *


Node *
p)
q ,

{
if ( p ! = NULL )

{
Reverse Cp , p → neut )
→ neut
p =q ;
}
else

first -_
q;
}

C0NCATENATlNGTWOL1NKED#

p= first ; Oln)

while ( p

neat ! =
NULL)

p =p

neut

→ neut second ;
p =

second =
NULL ;
MERGINGTWOLINKEDL.IS#

We have two sorted linked list and we want to combine it into a


single list .

if I data) O ( Mtn)
first → data a second

{
third =
last =
first ;
first =
first → neat ;

last → next =
NULL ;

}
else

{
third =
last =
second ;

second =
second → hent ;

last → neat = NULL ;

While ( first ! = NULL IL second ! =


NULL )

{
if ( first → data a second →
data)

{
last → neat =
first ;
last =
first ;
first =
first →
neut ;

last →
next =
NULL ;

}
else

{
last → neut =
second ;

last = second ;

second =
second →
neat
;
last → neat =
NULL ;

}
}
if l first ! = NULL )
last → neat =
first ;
else
last → neat =
second ;
CHECKFORLOOPINl.lt#EDLST

→ → → →

#
O ( n)
loop

int is loop ( struct Node * f)

{
* *
Struct Node p, q;
f
p=q= ;

do

{
→ neut
p =p ;

neat;
q= q
? neut
q q

q;
=
q
:

} while l Sh Ld ! -
g) ;
p q p

if ( p ==q)
return 1 ;

else

return 0 ;

ClRCULARUNKED

① → → → →

HEAD

② → → → →

DlSPRCULAREDL1ST

hyoid
( Node - p )
Display
do
Display I Head ) ;
{
" ''

print f l t d data) ;

p
-
.

p =p → neut ;

} while ( p ! = Head ) ;

}
DISPLAYCIRCULARUNKEDUSTUSINGRECURS.IO#

Display p)
*
Void ( Node
{
static int
flog = O
;
if I p
flag
'
! =
Hea ii =
O)

flag =L ;
data ) ;
''

t d
printfl
" →
-

, p
Display ( p neut ) ;

(
REATlONOFClRCULARLlNKED

Void create Lint AT ] ,


int n)

{
int i;
* *
struct Node t
,
last ;
Head =
( struct Node *
) mall Oc ( sizeof (struct Node) );

Head → data = A [03 ;


Head → neat = Head ;

last = Head ;

for lie l ; i en ; it t )

t =
( struct Node A) mall OC ( sizeof ( struct Node )) ;
t →
data =
Afi ) ;
t →
neat = last →
neat
;
last → neat =t ;
last =t ;
}
}

int main l )

{
int AE ] =
{ 2,3 ,
y , 5,6 };
create LA 5)
, ;
}
INSERTING IN A CIRCULAR LINKED LIST
- -

*
void Insert ( struct Node p, int index ,
int n )
{
*
struct Node t
;
int i;

if Linden 11
so index >
length) )
return ;

if Linden ==o )
{
+ =
( struct Node
*
) Mallo c ( sizeof ( struct Node ) ) ;
t → data =
n
;

if ( Head .
= = NULL )

{
Head = t ;

Head →
neut = Head ;
}
else

{
while L p → neat ! = Head )

p =p → neat ;

next t;
p
=

Head = t
;
t → neut = Head ;
g
}

else
{

for Li = o
; is index - i
; it t )

p =p neat ;

t =
( struct Node
*
) Mallo c ( sizeof ( struct Node ) ) ;
t → data =
n
;
t → neut =
p → next
;

neat =t
p ;
}
}
DELETIONINCIRCULARUNKEDIintbele.ie
( struct Node *
p , int index )
{
*
Struct Node q;
int i ,
n
;

if Linden so 11 ( Head ) )
in den >
length
return -
l ;

if Linden = =
I)

{
while Lp → neat ! = Head )

p =p → neat
;
u = Head → data;

if ( Head =p ) Itf there is


only one node
=

free ( Head ) ;
Head =
NULL;

else

p
→ neat =
Head → neut ;

free ( Head ) ;

Head → neut ;
=p
}
}

else

for Li -0 ; is index -2 ; itt )

p =p → neat ;
neut
q =p → ;
→ neat = →
neat;
p q
→ data ;
u
q
=

free Cg) ;
}
}
INSERT IN A DOUBLY LINKED LIST DOUBLY LINKED LIST
-

Struct Node

{

Struct Node prev ;
int data ;
*
struct Node rent ;
*
} first = NULL ;

void create Lint AT ] ,


int n )
{
- *
Struct Node t
,
last ;

int i
;

first =
(struct Node B mallOC ( size of ( strict Node ) ) ;

first →
data =
A- To] ;
first →
pzev
=
first → neut = NULL ;

last =
first ;

fool i = l
; i am
; it -1 )

{
t = (struct Node B mallOC ( size of ( strict Node ) ) ;

t → data =
Afi ) ;
t → hent = last → neut

t →
poet last ;

last → neat = t
;
last = t

}
}
INSERTINADOUBLYLINKEDl.IS#

Inseotatanygivenpositionlvode
(1) Inseotatfode (2)

* *
t = new Node ; Node t =
new Node

t → data =n t → data
;
-
n
;
t
pzev NULL;
→ =

t →
next =

first ; for Li = 0 ; is
pos
-
l ; itt
)

first t neat

poev p =p

;
-_
;
first = t
; t → neut
=p
→ neat
;
+ →
prev =p ;
if l → neat )
p

( prevent
eat →

p → neat

11 To check if a node is available after

not
p or

DELETEFROMADOUBLYLINKEDl.IS#

Deletinglvodefoomgivenindexp
(1) Deletingfiostnode (2)

=
first ; p= first ;
first first
= → neat ;
for li=o ; i epos -
l ; itt )

u =p
→ data; p =p → neut ;

delete neat →
neat
p; p →
poev →
=p ;
if I first ) 11
Tf first is not NULL

first →
poev -_
NULL ; if ( p → neat)

-_p→dat§

p → neat →
poev =p poer;

delete p ;
V

if → neat is not
p
NULL
REVERSINGADOUBLYLINKEDLISTP
=
first ;

while ( p)

temp =
p →
neat ;

→ neut =p →
pzev;
p
p

poer =
temp ;

p =p

prev

if l p ! =
NULL dd p

neat = NULL )

first =p ;
}

FINDINGMIDDLENODEINAL.IN/TEDL#p--q--
first ;
Two and will
pointers p q move

while (q) at the same time .

{ will move 2 nodes and will


q p
ment ; 1 node
q=q→ move .

if Iq)
neat ;
q=q→
if ( p)
→ neat
p =p ;
}

FINDINGINTERSECTIONPOINTOFTWOLINKED.LI#p--
first
fist
100 110

120 130 ~
140

Iso

¥
While ( ! NULL )
p =

( Isth 1

push p );
,
y 200 210 220

p = second ; second

while L p ! NULL )

push Ldstkz , p) ;
while ( stacktop Lst ki ) = =
stack top ( Stk ) )

p =
pop ( Lstm) ;
pop ( Lst K2 ) ;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy