You are on page 1of 16

215C1202 DATA STRUCTURES

at begin
I n s e r t i o n

position
3) at a n y
Insertion
position
4) node any
a
Delete
5) node in SLL
Scarch a
6) c h o i c e . . .
2
12->78->29

Enteryour
->
34
TheNodes are: ->23-> using
SLL for all menu options.
program
Test the
above


Program 4
aMent
fintlude
>
imih.h
<
sinlude
i n c h u d e k m u h r h h >

inude tshdbal.h
t i n c d d e L s k d i o . h s

4nchude esd t b h

i n t m dt eshringh

li
dhw yoad ine dlistnod Doublytnked lyt no de
sruck Dously nke
hypedef Lnbebs Doubly Lnksdit
ypece shrutt Doubly
inksLs rodef
shutl Oouby
M dada rvede neat;
Donsynkid kst
de prev;
Douby linkd kst ro
Jhutt Dowbly inkadust
Douby tinkud kst rNode head;
*

Ooully twl st nude aàl

node -> douta node-dat


noc->nen nULL
ne-> prev NULL;
nhn nedt
no de
Dowdig krkud lyt yode node - Cest-doubly. &ktd nde ote

FCCsdouby krhsdtst ) ->had)


67
ori
uwked. la ).>be,d n
(doubuy.

bi ). >
lalsnts/ rorde
(dobly Lnk1od Ust)-3to:
& (*douhly Lntsd.
ode prev

douly bnd. tud)>haul node

fpvint (FPh, .d,


" node r datn )
noenode->nent}
(node )
pint (fph, " 1r) ep

Lnkad.n CDaublyiklbd Node notrl


void ee.douhly.
wl Cnode)f
Node lenp:node
Dowby bnlead ut
no de node-> nea
Free Cemp;

Doublynksdigt Node curc heod


daloenew->doa
wh:le (cuY -> nert NULIAL curY ->nea>
CuYCurY -> nek

new urgn
Chan m

Axta yealloe ( duso,date -ungnj


hoin dae)

3
21SC1202 DATASTRUCTURES

n LabTask
verse the
the list without using the reverse function. The input and outot
Reverse a List elements.
Reverse

natically. You need to write a nction with the recommended method signature.
I. list of
handle

will
be
p o r t i o n s

r a n k . c o m / c h a l l e n g e s / f p - r e v e r s e - a - l i s t / p r o b l e m ? h _ r = i n t e r n a l - s e a r c h

https://www.hackerrank.com/ch

Program

ttincdude cstdio. h

i n t u d e c s t d kb-h>

shruttnde
int vd
shud no
de *new

S r u t t nsde
hea4 )
void pr-

pnt f(H->")
whle Chead

pint ("Y.d->", head ->vaU),


heud hend->nnt;

print F(lbnIn
void introt ( shunole** hea,int vJne )
Sut nede nt ade
NULL:
e-hod-(uct no de ) mallo c (te -

ne w-nde NUL) of Chucs noda));

(Fulee jnsnl le mend. ouk


mid. ou
nu of mmon
of mt moj"'
n. ->val
n-nde nnen yue
h
69
*had newnde

void m a )

int Counk 0, i, val


Shue nado ~ head :NULL

P A d e n numn of eltmedA: )

SCanf( d; &ound
fovlio ietonk ; ir7)

pint f(Enlen td th thmad,:/


Stany C7.d, vol)
insert-hont (Lhuad, val):

pvink H9ritos Lt: ";


pint ur Chaad
TeoseLnkd- Lut (4hund).
pvint e C"Rteyed t:"
pvint Lst (bendy,

ebmnt.c
Enar nume f
Aen oh h m u 1o
:

Saln qh elmas : 0o

uo
hen 3 cdemand
Ene y en :50

->/1(
->lo-8- >3o ->ho->s0
lenned L
21SC1202-DATASTRUCTURES

list
linked
doubly linke list, reverse the order of the nodes in place. That is, change
Reverse a node ofa doubly the direction ofthe list is reversed. Return a reference to the head
2. to
the.
head
so that
pointer o fthe
Given
the pointers

andp r e v
next list.
that the list is empty
the to indicate
r e v e r s e d

node
ofthe
node
might beNULL
head
The
Note: below.
F u n c t i o nD e s e r i p t i o n

editor w.
function
in the
reverse

the
Complete p a r a m e t e r ( s ) :

head of a DoublyLinkedList
following
to the hea
has the a
reference
reverse Node head:
D o u b l y Li n k e d L i s t N o d e

reference to ithe head of the reversed list


Returns blyLinkedListNode:
a

https://www.hackerank.com/challer
erank.com/challenges/one-month-preparation-kit-reverse-a-doubly-linked

list/problem?h = i n t e m a l - s e a r c h

Program
Hinchuk cstdio. h>

kb h>
ntlude es
Shrut node

ntdabr,
shut Node Aeat
shr NodePev

VeteSe (shrud node*head reff


vhe (cunr enk w0L)

mp: Cunent->pV
CuYYen-> prev =uent
- Nn
wYn> ntt: hmp

(lhmg 1-0DLL)
heod-amp->prev,
70
Void pubh (shuct Nodehed -rt}, iw
new.ada)
Skutt Node new-node

Ctkut nodemallot (* t (shut noel


new node ->dade = nuw-ak

ne-nde ->mev NUL


new-node ->nent: (*head
-red}:
(htad-) NVLL)
head- vef ) new-nade
:

Vod nt Lit ('shuct Node node


twle Cnode >NULL)

pwC(4., node->dadh ).
node node >nent;
owp
Gn s
mant)
I23 S6
Stru Node head NULL Revnsed A

push (Lhead, 2); 32 65 u 9&

unh lhesd,u)
push (Lheud, 8)
push C hd, lo)
inkad tst
PrtCh brigin
prirtlyt Chnd
ewnse (kinc)
Reverte linkd kud tt
t
prrttl In
21SC1202-DATA STRUCTURES

Linked
list
Doubly
that contains integers as the data in each node. These data on cach
1linked list
Circular c i r c u

ardoubly
special algorithm that prints the three continuous clements of the list,
l a 1

3 a

have
g I v e n
developedas

and runs for infinite time. For cxample, if the list is


head ofthe list
You
{1,9,12,7.
are

You
n o d ei s

or
d i s t i n c t .

element
first w i l l be {1,9,12,9,12,7,12,.). The output contains the infinite number of
trom
the algorithmn

of
the You
Y o u are given only a part of the output that has been returned by the
starting
output
list, circular

the number of elements available in the original list and print the respective
the a
then it 1s
because

determine
elements
task is tto
Your

algorithm

elements.

hackerearth.com/problem/algorithm/hiddent-doubly-linked-liste8c l feadi
https:/ www.hackerearth.com/p
w

Program
tindeude 2stdio.hs

intmirt)
0, k
int num
Sanb (d, num)
)malloc (Jite ind ) °num)
' n t - L s nt
( t i e df lin)*num)
)mullo
t ullt (int
7tT)
for (i0, < num;
Stan : (W+1)):

1cenum / de Ls 3+2]): lb +*
kov (1
wlt (it : est [3 +7)

thn o
71
21SC1202 DATA STRUCTURES

S h I L
S s l o n

I .R e m o r eF r i e n d s

be
h cr
come celebrity
a
at her univerwity,. and her faccbook profile is full of friend
hus
h us
Christie
has accepted all the requests.
C h r i s t i c

D,
PhD,
is, Christie
getting
her
t h e nice
irlshe
girl.
Aner
Being
she is getting r o m olher guys, so he asks her to delete some of the
uests.
ll the attention
requcs of
jeulous

is
Kuldeep
Ilist.
fricnd
w
Now
lrom
her
some friends from her friend list,
since she knows the popularity
guys Acides
Christie d ecide to remove
scene' uses s
algorithm to delete a friend.
the following
avoind
a
she has, she
To triend
of the Friend.length-1 if (Friend[i).popularity
:DeletelFriend-false for i
I
1 to
rithm Delete(Fricnd):

delete i th fris
cteFriend-true break ifDeieteFriendfalsc) deletethe last friend
1)popularity)

Friendit
https://www.huck
data-structures/linked-list/singly-linked-lis
nckerearth.com/practice/da
-list/practice
problems/algorithm/remove-friends-5/

ProRram
Hntlude shdio n>

;ntludce SPdl:lb h>

nlraut hp;
nt man(void)

ht nk, num
Stanf(rd, &E)
Lkle Ct>: 1)

ftan C1J , dn, RE);


auk: (ins -allo c (n, i x oA (in))
Ston ("", 2numJ
tT
Stauk P) : u
fov (int)j jen; 1t )
Snt C7. Cnu

14 num
SutChp Jia toP -1)_ 74
op

op+
SPLk roP): nwn

We k:1)

r(int i:0; ic bp; itt)


phint (.8, 2mutli)
opuh
ee (std ;

3 uSG
- DATA STRUCTURES
21SC1202

list. Given
is visited m o r e than
once
while traversing the a

cu cle if ar
cycle if any node 1. Otherwise, return 0.
2 .
C y c l eD e t e c

said
t i o n

to
contain
a

determine
contains
if it c ontain a cycle. If it does, return
is
l i n k e d
list
ked list,
linke.
o fa
A head
the
to
pointer 1->2->3->NULI
list so return 0.
data values. There is no cycle in this
ofnodes
Example

not their
tothelist numbers,
headrefers shown
are
the
2->3-> -> NULL
-> 2
The
numbers
nodes 1
to the
list of back to n o d e 1, so i urn
refers points
head node 3
where
cycle
There is a
F u n c t i o n D e s c r i p t i o n

editor below.
function in the

Complete the has_cycle


parameter
the following reference to the head of the list
It has head: a re
pointer
lyLinkedListNode

0 if there is
Returns not
or
there is a cycle
- Int: 1 if
https://www.hackerrank.com/challenges/detect-whether-a-linked-list-contains-a-cycle/problem?

h>
de <stdio
Program i n t u
Singuy nkud hst

int dat
Sinolunanteds node neat;

bool has-Cyue sing y inte Lu nwde heon)JS


nt Count:0
while had 1 roUL -

heas had >nerl'


Caunt +t
it(tount>99
thon 1

Dthunn 0,
75
21SC1202 DATA STRUCTURES

the next pointers of the nodes so that their order is


list o off aa
o linked list, change
linke

that the initial list is empty


node
linked

null meaning
head

R e v e r s e
a to
the
may
pointer given
3. ->NULL
the
head
pointer 1->2->3->
referencing the head ofthe list
thelist now
Given
The refurn head,
reversed. es
in place and
each.node
feach
E x a m p l eh e a dr e f e r e n c e s

pointers
of
the
ipulate
3-2->1>NULL.
beloow.
Function Description
the editor
ion in
f u n c t i o n .

reverse
Complete
the
parameter:

reference to the head of a list


following
edListNode
the pointer head: a
has
Reverse

-SinglyLinkedLi
of the reversed list
r e f e r eence
ne to the head
a
pointer:
Returns
i n k e d L i s t N o d e

Singly hackerrank.com/challenges/reverse-a-linked-list/problem?h_reinternal-search

intunds ejtd jo'h>


(sinal.
o n S e (singy
gowse unked ls a he)
Program
bst dnode
Sngkyknked p
prru, Cun,ned
pvode
lal
Swgly knked
pveV N U L L ;

n NVL
Cuv = heas

uhile tuN

nexCurnerd

Cuy nent pev,

eV t r
CwYneu

76
21SC1202-DATASTRUCTURES

list
linked
sorted linked lists. The data in both lists will be sorted in
sorted head nodes
node

of two
mo the
Merge p o i n t e r
to
to obtain a single, merged linked list which also has data in ascending
4 the

next
nters
pointers

gethe hat the corresponding list is empty.


given

Tre
You
order. Changei

benull
meaning
a s v e n d i n go r d e

given
may
m ay
pointer hallenges/merge-two-sorted-linked

www.hackerrank.com/chal,
Either
head

ier.
hs
Programttinthe
.de
de m r Node
Stdro
lah (ringlo Lnkud ist rudehe,d
hs

Sinaly unked i t No de head 2)


knkd

Sngy
l
LIF n u s
h u d . malle
(singy Lnkud us)
nkud
fnqy ULL
ne Head-had: NULL

lail: nvIL
N Head nJuLiL hens 2enUIL)J
while ( hesd!
: l-^dJ}
hexd
daba >
i Ckead1
node-inb-Singy-inkd
-

bi (2nes Head, headie1-da


hen
ngent
head 2 head l->net

/ - deka cheas 2 date)t


tbe it (head
intentnode -inh -ingy- inked _ ist l &neshee d, heads

htdt htod 2 nrt

inere -node -into_tingly-inttdlat (Bnew Head, hediy


Unt_node -inho de-ta),
-

gngly- inkd-w (Lnu Htad , huud e

hend 2 ht ad 2
nent,

while hend1!nuL
no minh Sina -liskedst (Lneshens, herk?~
L
headhead 1 hex
3
whe (head 2! wUL
C Lnes hoad, hendes
hent_node-inh-Singy-uotud-lst dada
head hea d 9hew

ehun he Head ^htad


21SC1202 DATA STRUCTURES

linkedlist
headofa node before the head. The next value in the
new node
at
deatthe
the list,
inked list.
insert a new

reference to the
linked
node new
a ofa with a given value. Return a
Insert the
head
should eplaced
5. to
data
value
is empty
Given
a
pointer

head
and
the
be null meaning
that the initiai
to given may
point pointer
should The h ead
list.

ofthe editor below.


AtHead in the
h
Feua
ndctionDescription

i n s e r t N o d e A

f u n c t i o n

parameter(s):

the following
mplete the
ofa list
Comp

has
insertNodeAtHead reference to the head
SinglyLinkedListNodellist
new node
field of the
fie
insert in the data
com/challenges/insert-a-node-at-the-head-
ad-of-a-linked-list/problem?h r=internal-search
value to
https:/www.hackerrank.com/cha
data:
the

Program t d i .h
d). hs
h e a dhtadhre
ly inked L
a t ha
Noe a had(sing
deine po
Linkedbpé
Node inno Node Lst, int dade)
Singly
Lnked ly
maloc(d*
of (inguy

Sinaly bnkeclst
node
new
pode)
ne data : duta,

afnev
nw nerl-\\o
vehwn r;

nt nent hst

78
21SC1202 DATA STRUCTURES

and a specitic poSition, detcrmine the data value at that


Value of a linked list
Node
to tail is at position , its parent is at 1 and s
The
6.Get node.
p o i n t e r

a the tail
G i v e n

from
C o u n tb a c k w a r d s
rds

Exanple refers to from the tail. The value is at the desired position.
n a tches
its distance
values
match
data
e
Function Description

in the tor below.


function
Completet h eg e t N o d e
p a r a m e t e r s :

following
has
the
refers to the head of the list
SinglyLinkedListN.
getNode pointerhead:

retrieve
to
the i t e m
p o s i t i o n F r o m T a i l :

int

Returns
position
desired
the
value at
https://www.hackerrank
int: the rank.com/challenges/get-the-value-of-t
f-the-node-at-a-specific-position-from-the
t a i l / p r o b l e m ? hr = i n t e r n a l - s e a r c h

dode(sin9y inked
isb Mode * u a , int p/t:on

Tarl)
int C:1, i:l
bnked tse node ~
phr =hend
Sinqly
ohile (per nnt !:0ULL

ph p h - > n e t

F(pos:tvm hrow hlz o


rehnn ph dut
phr: had
CC-potun fom Tail)
ohilt (i c)f
phr pbr-nerl
uan da

79

You might also like