Adobe Scan 07-Mar-2022

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

n Lab Task Linked List

Doubly integer, data, create a

Insertinga Node
Into a Sorted of a doubly-linked
list and an

L. reference to the
head
insert it at the proper
location to maintain the sort

Jiven a data value data and


having
DoublviLinkedListNode
objcct
ew

NULL
ixample list 1
<->2
<>4->

fead refers tothe


ata 3 <-> 2 <>3 <> 4-> NULL
list: 1
etum referenceto the new
a

unction Description in the editor below.


function
sortedlnsert
omplete the
ortedInsert has two parameters list
head: a reference to the head of a doubly-linked
insert into the
oublyLinkedL istNode pointer DoublyLinkedListNode you must
the data field for the
nt data: An integer denoting
the value of
ist.
eturns to the head of the list.
a reference
oublyLinkedListNode pointer:
ttps://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-linked-list/problem?h_r=internal-

snutt Doubly l?nked nlode


earch
{asdert-h>
rOgram # mlude
#include <Lmft3.h int data,
Ooubly!rnkedLis Nlode Y ney
#fnclude { math hz
DoublylikedtRtlesepreu

#include <8td6elh
#nclude s<Stdib-h> 3
Hinclede<stdl?b-hz 84ct Doubly Linkedlfs
#include csring h> Ooubly/nkefsfalode hend
Doublylfnked tis tNbde tal
cha neadlinet);
3
ypede sbutt Doublylhked Cheafe
0oullylinkestst alode
-

tst slode bublyLnkellktakale, R-nde(tntnmie


sF nted
Detuly/nkell doubly
pedptuct
noubly Pnkal1 da fa = rooe da ta
node
node-nerf Null
54
Crode)
un node

nede.- ide-doub
Bert
inI(0ouiylinked tR*
erdeuel
COublatnhelafnio
node
(Dulytinkeltfploke=
tfnked
Ltfalode
e -deublyRnte0S-node le(node
ly
Ooubly nedttlot
node nodener

sked -rok Ceefemp)


oubly- AFnked_at
lai/>te
OhkelLRst)
doubly tnacHOoublylînkala/

neonerr=head
head-PhcU = ns

Cderbl niked.st)>tsknele, 3
e l r neu

id pint_dsukly 9ked.0RAOouy STOIN Euncdon

node data y

dal5
2 . D e l e t ed u p l i c a l e - f

nkea Ist
ter to
to the head node of a
the
poiter
sorted linked list, where the data in the
1 are
given
return a sorted
sorted list with cach nodes is

given headascending
Delete
nodes
and
distinct value in the original list. The
order.
that he list is empty.
ay benull
indicating
pointer
Exampe list 1I ->2
first node in the list
rst node in the ->2 ->2->3->3>33-NULL.
. s

head refers to the


2 data values
data values and return head pointing to the revised list
a
1 ->2
Remove I of the ->3-NULL
Function Description
function in the edi
function editor
Complete the
removeDuplicates
below.
has the following parameter
removeDuplicates
nead: a rererence to the head of the list
Singlv LinkedL istNode pointer

https://www.hackerrank.com/chailenges/dclete-duplicate-value-nodes-from-a-sorted-linked-
list/problem?h_r=internal-search

Program
Hncludkasth suenghlealsdaldo)
c ude <m?ts h> Rnt dofa
4ncude <math.h> stngyLRnleed Liat Albde*nexf
neude <stddh
inue<StAfat-h stauet strgLnke Lsf
melude <s4doh>
3ha LRokes Lisfalsko* he
inc ude <stt s Rnkelstalsir{al
4 cde sAatra,h
choeadine)
pedeshuct 8gyrked
tsA alole stal hkes Cs sloke
noeaiq no data,
eeeaut straynkes
Rs SnglaLRnketisk node ne xt -uL
etu no
5
2

U
1 - .t
C

iaha |
21SC1202- DATA STRUCTURES

ked listusing recursion


linke
themiddle
ofa given linked list. For
example, if given
linked
then
middle of the
3.Find list, find

be 3. nodes, we
need to print second middle elemene
nt. For
would be two middle
should
output there
nodes, then should be 4.
4->5->6 then output
1->2->3->4>

list is
linked
nodes in the linked
list
example, ifgiven should add
https://www.hackerearth.com/problemalgorithm/tind-the-middle-of-a-given-linked-list-using-recursion.
Note: Insert
function
on/

lash-pft snetdSred
Program
#ncluce zstdfo h> ak-phs
f0EL- h> slouops/ous-pt->ner
Ene lude (S

S/ruct n ode
PhintfThe middie elemen
N
Pnt dafa /d\ninl slow-P dala)
Smutt nlode"nexf,

head-
Mllskuct alode vod puahStauet alode**
.evo p nt
neus -daBa)
read) ne fnt

siuth alode sl o fto


heacd
shuttlodenetw -node
Sfuttn/ode*fast_pta led (sauthstode) maltloc(sie
head f atoLL) (struch Node)
dao = newda
e s node

o e fa-ptol alutl nejt -(*hed re


neo-nade

-nefJ =nesy-nade
C head

56
nodepér)
(s4utt
Ood pnLR
hile (pis A t l )

215C
pihtf 7 d pla-daka)

Task
Pla pla next co game,
Krishse, Vasu. Shiva.
to Anand
e rally tsg is given
linked ist of Single
circular
s a
shortest person in the
how is the

rotf u L \»")> ut Forma

thei s t

the list
shoriest person
in the list.
e
choice: 17
nter your
of the Player-
nt ma?nt) .2
name and height

=alULt,
sAut alode*heas 4.9
.5

nt; e the list

ax(1-5/0t-) lay the list


the shortest person
in the li

our choice: 2

ph(thead,i yers are

a
Height
5.2

.i
PhfntI (had) 3.3
5.4
4.9
5.5
5.0
enu ***

ate the list


play the list
Hen 07 nt the shortest person
choice:
In
3
it. Enter your
Sathesh
hortest player is

-34-S-alvLL
the mdale cduenen l
- 3-4->5- NuLL
Theidde duncnt C3
21SC1202-DATASTRUCTURES

Skill Session
LinkedList
Elements
ofa linked list, print cach node's ata element, one per line. If the head pointer
Printthe node ofa l
1. pointer
to
the
hecad

there is nothing print.


to n
Given a
is empty),
the list
(indicating
is null
editor below
W.
in the
Function Description

function

printLinkedlList

the parameter(s):

Complete
following
has the of the list
P r i n t L i n k e d L i s t

reference to the head


head: a

https://www.hackerrank.com/challenges/print
SinglyLinkedL
istNode
challenges/print-the-elements-of-a-linked-list/problem?h_r-internal-search

kassenf h>
ErouEa4tint
hude
tinclude limiksh
<math .h>
inelu de
tnttude <sfleokh?
#nclude gstddefh
include (sfdnfh
nclude Astlo-h>
ncludegstdl6 h>

Cha readline(); Lisf alode


Singlylinkes
Singy1nkellistalede
shruct Lsf
ype def 's Singlylinked
seutf singylinkedl
ypedc
Singylinked lst Node
shutt
in dafa;
gSingyinkedlisk node"per
huc Senglmketst
Singlyt Pnkedl isfatode head,
Singnkedlfsfrlode (al
59
srgyn kedtcf alede" cteafe_stng-Imkes
s-ode
nodedaBa o
node dafa,
node next- dul,
tetusn no

ved inse.node-inko sin aesrs|Singylnig


Enhedt, *n noke dof a
1akedsNode"nodeMeate s ?rgly tnhal
(ng.nteltt) ->heod){
singlyniked. 1%t)hec: nie
ebsef
Cs in aked.tis t)-s ta-Jnexne
3
Csiglytrtrog Rt) J fodl hode

Sinmrd Rs+ Node femp»head


ternp! autl)

pint edn", Iepsala),


mpfemp nex
21SC1202-DATASTRUCTURES

list of integers along with an integer 'N', Write a function to append the iant
list of
intea

1.Linkeds t linked

a
singly ked list and returna the new head to the list,
given
been
front
o fthe
https://www.codechef.comproblems/REC
You
have
the 05
towards

nodes
N'

t n c h e d e( s d r o . h >

Program:

f m e h u d e r s A r b h >

yPpeoe shruetnode
int dala;
Smuet node nex

dad
3 node"cneaft
ndelint
struet

struct node nodenew

rutt nede")
Newnode(S
matlocisiy Snuttnodr
netonode > NvLL stnuct ncde fe mp-head
while (lemp nexfl-a/vte
neturn Joll
temp femp nert
dala d la, 3
newnode
cturn nttwnode; emp-ne xt >ncuwnode
3 hctorn heqd i
ofmuetnede* n berfafendlstrud |3void print(sfnuetnosde *head
nodehead, tnt dala)
head= =Atvtl)
(hcad: ntull netern
Sfruet node emp-hea
*

head Creqke node (dat); Lohila(keme)


printf (1d",
temp-azkela)
Hc fwrn head,
temp kmp next
truct
pnintk(n
nedentnede cacatenode[dat), 60
nod
sfyutt
{intn,
nedeapprnd
mutt
break
b7ea K
int c- emp-head,

Sruet ned Ctt 21SC1202 6ar


Stmuct nede

s f r u c t node
emp:head

emp? :
head

3
head
endlhenà
inbetabendi
-inðerlaen d/. rtaNode at the Tailof aLinked List
egiven the pointer io the head node ofa linmked

Yneztl:ntoty e given integer. Insert this node at the tail of d

ohle(tenp2 afterinserting this new node. The given head pe

empa: hemp2- e r Szanfl7, 4r), on Deseription


ete the insertNodeArTail function inthe editor

3
o h i l e( l e m p - > n e r t l : n l y
KCUk>o) odeATail has the following parameters
LinkedListNode pointer head: a reference to th

ta: the data value for the node to insert

ctti min t(headl) /www.hackerrank.com/challenges/insert-a-nod-

am
lude aserth
else {
bneak append[-k, heclude l'mik.h>
emp: lemp-epl; head v/udekmath h
eleude 13tdkoc h
3hecad temp-)ncxt
n et m n 0 clude 1StddePh7
femp2 nezt: templ; clude<stdnt h
emp p e t ialvll clude s teio.h
ekadeks tlh
| Pint(lhead), c luede As tring.h7
npd
int Prainvoid) { readlihe C)
int t eolef stmuet Singlylin hL
n 5
Scamf('d, te); 34lhkedlistrlecle
"

3,4
ohile(t--) data vaus =t, 2,2, ede f shut f Singly inkedl
nglinkedLt,
S/ruet nede"head »alellopu lafale
uet Sngly Lnked
int C: Oj n data
oAle () Singly tinke3talede

Pnt n struttsing lynkessf


ScanfCd,tn) SinalyLinks .fNedehe
You are given UIe Oue of a
with thegiven integer. Insert this node at the
linked list and
the tail of an
formed after inserting this new node. The given hea
the
linked integer to
add to
list and the
Function Description pointer may be return the head ist. Create a
ull, node of new
node
Complete the insertNodeAt la unction in the
editor
meaning that the initial
the
linked
insertNodeAtTail has the following parameters:
low. fist is empty list

-SinglyLinkedListNode pointer head: a reference to the


head of a list
int data: the data vaBlue for the node to insert

https://www.hackerrank.com/challenges/insert-a-node-at.t
Program
Hinc lede{astert:h>
e-tail-of-a-linked-list/problem?h rinternal-search
1Singlylinkedlistnlode7
nclede <lvrîks h>
kma th h>
Chealesingly-linked lish_node(?nt
includc
#Tnc lude {stdkosl h Singlylinkedlistwod
malloc(sig of
et pode
nodo-ata)
einclude <Stddfh 7 singly
node data =node-dta nbods) linked[kh
nclede sfdint hz
node next lutl
tinchude (s tefo.h
c tuna node ;
fnelde <s tHl:b h>
Snc lude <s ting.hy void prinfusindy Inked.I3t
cha readliheC)i CSinglylike list nede nodeC hod
s/nuet Singlylin kL%/nkeieE fpbr) FlLe fp fr)
ypedef
whfle (node) {
SPnglytnked Listnlode
ypedef shuet singlyinkedl&t fpaint F(fpfs,*7d ,noe-da
node node -neyt
Singlylinke dL&t,
struet Singly binked tfnlede (nede)
n dafa
fprintk Pphs7S,Sp
Singy tinked/3tatodenexl void ree-s/ng -linked-1t
sbrutf singynked/& ff
Csingly Linkel Lisfntodet node)
SnglyLinkel[?sfAlede "head, shile (nede)
61
Srng lynk edli's t Nodet lenp-node it[¬zrl_FAn.
TFALLUR
node cde ->ney l> fn(int:o, iAllR/.
reeltmp) chod 11st-er
hat llikt-hmsstsesA
Sing inhed lis/Nodenbelninlatatla
ho)
int llis? lem sfa lolel (tSsti
iy inhed(Rt atohed,
Plen -cndpts,to).
(uistA hem.endph.
head: Note) {
e furn nislem-Sfs \1" th/
e
Chea le_siny 1nked.13/ nde(hlo), 'o"
Singytnhedlsttsly,
singlyinkedl3t Node nede hrud,
=

(-Alotu{ Finontnlode ABra?l((uisl


while (node next
node node next Chan bep ="\n",
3 Ph ntsingly-lenked-lis,
node net Sep, Fpto)
CHea fesnglynkel 3 tnode (ala), fprin fCpft, "\n"),
Te fenn head reesêngly-lnhd.la
3 PclooaCfps
int aint) ne fnn o
FILepts
nlgchanv"ouTfUT.PATI1)u
siglylnkedl%t"%A outpt
nlleo(Snayinked tk)» 3
Lst head= aloll, 5
axist_Countendtk
chan l/s t.count st readlYnet
\;\
fnttsl-tount »sfatol(Esh-ccunt_sk
1isfcount-endpha, 1o)
4tisf- Coun /_endpfs
SE =cocont. stalttrnst_caun cdfto
21STI202 -

DATA
STRUCTURES

a Node
posilion in a linked list and return a
linked

4.Delete
Delete
the
node at
a grven
after you e delete the node In that
case, return
referencetto the head node.
list may
be empty a null value. The head is at
0. The position
Example
L h i s t 0 - > I - > 2 - > 3

Position= 2
, llist
2, llist =0
=0-
>3.
removing
the node
at position
After
Function Description
ction in the editor belov
deleteNode functi
the
Complete parameters:
following
has the
SinglyLinkedListNode pointer ilist: a referenc
deleteNode
the head node in
the 1st
of the node to remove
the position
-int positio:
https://www.hackerrank.com/chalenges/delete-a-node-from.a

Program
l enges/delete-a-node-from-Singylink
a-linked-list/probledlist
em?h_r=iNod
nternal-search
#inchudecaxM orlh
inçlude <lmits h>
node dota
include math
h>
Fnclu deestdboolh
snay tinke dlist alodened -
inclhe (sfdetf-h> wallos(s ct (singly hkeafsf nlbd
Enelude s fPnf.h> node-dakq -ndde -dalo
nclud e RsfJfoh> node > next
include <SJl6 h>
=lutl
3 euin node
#ndude <stzgh ofnset-node irdo -sirglynk.
chat neadlinet)i ts
1-CsPenkedLs7" sirdy.th ke
typele- s/rut sinalinlediode sfnt node -do
snglylivkest Ktede ; SnglyLinked L84 Atode nok
redelsbructSingyrkelt cHete-ing n ke4sf-nede(ned
ra linbeJlst
Stnueh SirgyLPrkedlst rlo
int dala CCsng hksd t)hess
Sfhgynkedl+st dalo* nex
3 s y nked aed Aode
sHuctstydlinkel tiSt
SinginkedLRstMsdethead Prt
Csinq y- 1hked-le)->4alode
3
CSingy-mked st)stail: node
62
epit S,led 8/yVO
malloc
tivkei&tModnde chab Sep
us-heod lull,
&lelnode 18 1o Null;

Yode nae next ehot1skCount-y


nadlne),
nede) nt lst-fou

'vod ecsi nked- (ounftndpts Jo),


013-(cunttnspe :[lies
nke stNodede
ede
Stho
Mode? lempa txF(ExIT-FAILURG);?
keaLRtnode
nde:node, >nert (niok |t.coanhji
eeen chontps-fem -endpltr
chottst-ikem -s^-
St nkeaL?s AA
delehealtde (sRaukeatBAAl meadlinet
trist,nt pe lron) fnt sem»Sa el1
le-st rfhstifen-trdpo,jb
(fotion )--o){ fn &ert-nsse.nto sal
3tfon ttist >nex tinked ts
tlishnex enrst, rist -
elelenledenSnextedilan-)
efwnllist
,
Fn marh¢ )

FLepho
bpengetenv (bupot.PATM'), |
21SC1202 - DATA STRUCTURES

Node from 1 to N. The tail of the list points to head of the list i.e.
Kth
n u m b e +

N
nodes
ked
5<-" An integer K is also given to you. You start
Remove
contains

N=5 |->2->3->4, A
5. list cont
list For when
A linklar in natd when you reach Kth node in the circular list you remove that node. For eg. if K-7
A
linked

we
istfrom he
counting from head.
ht now nd move K steps ahead till we reach node numbered 3 in circular list 1->2-3-
I rig
this node now the new linked list looks like this 1->2->4, .
i.e. node
head
from
We
remove

Node
start
S -

now we start counting from the next node. This process is repeated until
> 5 > 1 - > 2 - ]

has
been
removed

ber of the last node.


only
umbe d 3 Your
task is to
determine
left.
is
Node
v.hackerearth.com/problk
oblem/algorithm/remove-kth-node!
https:/www.hacker
one

Aaserkh,
h> sfrglynkedlist nlcdY
ineleude
h>
tlm s
roem

fnlude Cneak-sngly.nked -l36-nade(inf


#
incfudcma th h>
lode-data)
# ihelude ts/hoel h> 9vytfnkealistNodek node
Hncude s fdioh2
malloc(sf sr tinkedL`t lde))
ncleds<std/?b sh> nde daBa node Aa la
Fntede 4s/rtng h? nddenextrtvll'
ha reaslfnet )> 9tekunnode
y pedef shrutt sigylinkd void îhe-node-ivto -Srg
salode
sinalyinkedl&halode, fst,int ne de -dafa) S
pede f otuc hs inglylinkellst
3ngnkealist nfo de node
sng linke dlfsf
Ceale siratnked .made
shust singlytinkedlis/atede ncde daa)
fnd dofa
aly Lnkedlis/ Alodke,"nex singnke3head
Siay hke t t )>head
Shruc sinas1oked lsk noder

sinady inkia!Stntede*head) ee
RyPrked8f)s1Snext
Mode

63
-oid pint sing-nkeds
SRrdyLnkal& slodenede dan
LefpbX
feots-_endphs,lo).
encp
aPlehede
11 eols-endpa/-1
pH b7d,ned» skaha),
noeode-next, SexftEnT-FAIWA
Cnoe eho lst-Coant
pin Fp7S, se)» ehot tr'sfl-tounlsh.
eadlneC
inl tsLcoum
sinayLRnkest aiade* pede stn (A-counksls,
tlenede hisf-tount-endph,1t)
Srynke Rstnbde" kmp:
node
istl_ee ale endp'
-lst-Counf-st1)
ee femp)
be Comgone \i|srg
cxerm-FAlLURE)
neN 'st lodot eci, ehont tisk_km.s}a
Rsaode7 head ) SEgt?aked ncadline O)
3
nt ma nc )

fepergefenv("corpur- PAT")
eha iss-endphs
215C1202- DATASTRUCTURES

lists
Compare twolinkedtothe head nodes of two linked lists. Compare
thehea the data in the
5. pointer
the nodes of the linbodL
You're
given all data attributes
cqual. .
are equal and the lists are the same length, return 1.
to
if they
are Otherwise return
check

Example
llist1:1->2->3NULL

llist2: 1->2--à3--à4-àNULL

aequal
l data
lists have
data attributes for the first 3 nodes. List2 is
longer, though, so the lists
The two are not eaual
Return 0.
Function Description
parameters:
has the following
Compare lists
SinglyLinkedListNodellist a reference to the head of a list
SinglyLinkedListNode llist2: a reference to the head ofa list

htgps:/www.hackerrank.com/chalilengeicompare-two-linked-lists/problem?h_r=internal-scarch
Erogram includc KaeThh
ue gnenkedtst
h fnclude klimi fs.h>
Stnayk ejLi nlo*head,
ncude {math:h>
Anclude (sfdboo].h> soLnkedLRgfac
#include <s fdle f.h > ndy tinkedf'st alode
ntude {s tdfnt hn Ceete -Sfnal-1fnke -1-node
n clude sfdo h> Cnt nede -duda)f
A include istninq.h>
aninked tistnlodense
cheor ncadlineC )
Malte(gABray*mlsedtAlalt
ypcdcf shuet sfnglylnked
nodedoia -hade_data ,

MoAe
Ipedel squds tauAMMeAn etuen neder
3
gfdhent - node -into -SRg
3
tquchSera utkLPÅ rMd

ntnkell?sh lek.'yed SFrg nke 1 t node


21SC1202-DATASTRUCTURES

nfJst-ceuotstie [RsE -(eunt stt, nd

,I
trscount-end fi> rllsSh-count-san3 reunt
endfslo')
Sex(exIT FANVRf)

ehat lli.°\en -endplt


chot lt-en -Bt= 7eadlfhet),
nt t-fem=
sht(ts-fero_s, tllak -ifem _erdphso),
nberr-node --nto-sfinely nkesHsfd ufsf
Lfst-flenm

use only)
(For Evaluator's
Evaluator's Observation
Any out of
Comment of the Evaluator(if Marks Secured:

Evaluator:
Full Name of the

Evaluation:
Evaluator Date of
the
Signature of

65

You might also like