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

DST Week3

Uploaded by

jovialgoodall4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

DST Week3

Uploaded by

jovialgoodall4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

23SC1202- DATA STRUCTURES

WEEK-3

., ..h S ~

pate o
f the Session: -
I
-
I
--
Time of the Session
- to-

. . linked li~t? What 'ru-'e the primary compon~nts of a r:iode a singly linked list7 ..
What is a sing1y tt () • , ,~ , .. -
1.
st..v-
it ut1d,re..c-Honol ,.e_ , t Cof'l ~~ev..St.cl 1n o,,h,. ont- d ~vu.tion
_.J

~d -to -ta\ I • Pa to 4 . po~,rf;c_,: . . • O"'- • , pr: tl"l()-ij , eo"'1 .:


. th different types of linked list?
2. List e ,, t "
--t1ik l•n~~ tu
f:bqbk. ~niid t!..st
c_,qrtul-,f'/ lft1 d tr~ f
3. Write the operations name which can be perform on singly linked list? • ·'
k,°tio" o.l ~~,,_~,, 1n~vt1o,.. .. ot ~Ht6n rJn..lt1~'0f l ... o·t end,... ~k-lt o+
½',,.i~ , leil! ot P~~-,;'on , ~kit Qt- ~,,J •; ~W3 •
4• How does a singly linked list
differ from an array in terms of memory allocation?
.
~~kd lt\i ~'bit ~,wnh vor1dom'.J o-4 OnJ ' • f ,th!· m~mc~
~ " - Ill j.,, ONoj "VltMe'I__J J.~H. , f..! .. .-ff'kJ " ,"'?~~\t ,.
d<t. b"' o ..f.ion
5• What is the •
pnmary advantage of a singly linked list over an array?
.
r
..lvt5u-\ion '; . I
eJ#tii;., ¼
6nt\ c1~k~~"' t!>pe.,o i,'O(l_i ()'('{ "
~\t,'I Ot'ld ~{

fit1~td 1..1 ~to o-r a .Spe.~i /:I'-


P-,o"'1J~ -!ln1bl~tr~ 1n,i .fn.st1~'1 j
I I ..
ft~Hf6n {' de. t~ ti(Yl ol do+o·
!
.. -(,-om 31vw ~ •(>#O<'),

,. . \ .I .
I

'l • '

I
I

j
23SC1202- DATA STRUCTURES

In-lab:

1.You ~e give~ the _pointer to the head node of a li_nked list_ and integer to add to the list j
~ode with the given mteger. Insert this node at the tail of the hnked hst and return the head noci C~
hst fonned after inserting this new node. The given head pointer may be null, meaning that the ~f
empty. e•

Link: h s://www.hackerrank.com/challen es/insert-a-node-at-the-tail-of-a-linked-list/ robJe!.ll

Program: -
1- 0, I5trJ t-~d I P,,, t lb ia)
, . ..S:"'jt~ U'..!~ ~t. . P,n~ert , )')Mt -At~, . l:: . .. . .. ,
: l -. • • i. • 'f"" , I. & f • ' " .. •

l t . . .. .
.SLJJ •i •• ~w ~. (l toit •_ Lf\l l<!l{a))

(h~ ::. -: ~uu,)


I •
. . .. •

' .
-

~ht {
••• 5t..t~r.j.· ¼,,~J·;~d; .,~•;.- . , . i • r.. . ,.,· ....
'f.'L '. - ".J-. t ... ..1 L.l).
Wt'\\I~ tltn p
\... ¥\l r -" ! - I':-' • •• .. • t" • I,
.
r •' .,-,. '

.. .
•· •

•'
! I
) 1 •
,
o/r
.- r.

ft1)'.':!
-! 14 I

•' 302-· • I

30)... l 6Cf
Sample Input and Output: f 6 ll s~o
S-30 ltl~
~41./

48
23SC1202- DATA STRUCTURES

.
tr versing a linked list. Given a pointer to the head node of a linked list print each
. . an to practtce a er line. If the head pomter
• • nu11 ('m d"1catmg
1s • th e hst
• is empty), there is' nothing to
r111s is ternent, one p
z. , data e
nodes
print.
kerrank.com /challen es/ rint-the-elem ents-of-a-lin ked-list/ roblem
s:// ac
001<: h

'J
<

• or , 1'

.. . . . I
I
!
{
f~.,-lt 0_' ;/.d ',. t~p~
• c " l
'
I
I

. . ..
{J~n t: t L'' '(' ·'); • .. ,·.

Jct.mp t:,f.lTl·r 4' neY.tJ ,.,


. . . ' . ..
j

)
. *
.. ••

.. .

' .. - 4

Tu~ s
\6 .
. ...
l~

Salllple In~ut and Output:

. ; I I

49
23SC1202- DATA STRUCTURES

3.Reversed Linked List . . .


you are given a linked list that contains N integers, you have to print the elements of lmked hS t m the rever:
Input fom1at
• First line: N
• Next line: N space-separated integers that denote elements of the Linked list.
Output format
P?nt the N elements of the original list. . · .
Link: https://www.hacken-ank.com/contests/ds-week3/challenges/reversed-linked-list-3

Program: - .. 1-,' _srr,;,, J. (" 1/.d ", 4' IIU WI ' ; •' > •.. • : ' .
_st'N(_t {
Nod Pu.J h ( head , "um ).
ln-l ci,+-0 .
..
I

I , i • : "

,S.{'f ult ~ l ¼-V\eY. l,


....S·huc t Y\cd~ . ¥- , =.h(Od/
}, /
I
h~1t l -ierri..p ! NU u. ) {
'f~J P-t'rnt Rt~,s, 0-buLh,'t'd<! i heod) p,., f,d/ (";,,d ';-ten, p :...., d,');
\
f 4 (h(Od ~:: Nu~i.) 't!mf ! 1:tftl 1' -) tit -),l-) 1

'lty- e..tu Y n ,
) ,
/ N,, t -rt.YivJ~ ·Cl'leacf),
,/
.... r

p..;f,., \ Rt~sv .se Lh~c\ -'> .,t., t:-);

.$-\:'fut t 'It- 1\,.1 h (..ii'fuc. t ii\~ lJ.-he«\, n t ht~) {_


_s{'futt "'°dt, ~CAI >-wd~ (5.·h lit t ~e f ) %Ito~ &,r1.e. o.:/ ls·fn,~t ,-.Jod!});

,-_,~f~ (~tQd\ ~,Jui.z..)

~t,ut l ~cd(! ntw Nt>dt :: htoa --) ~1-t,


,I

f,,..-1: rrJ;r'\ ( ) {
1-·,u lhtQd );
·'
'
J-hutf Nt>~ Ne- h~ k ~ULL,j \rl~d ht~ c-1'\o<k .•
f,,{ ,J,
/
>lon-1- l"' X d ··, .( ~)J
{_f" l:, 'r' 0; f j f-H ) {
f n f- 'li-{ IYI)
Sample Input and Output:

50
23SC1202- DATA STRUCTURES

~ e n t a singly linked list having all unique elements with the followin g operations.
t.hDP
_ inserts element x at the end. ,,
1Ox _ If the element y exists, then insert element x after the element y, else insert element y before the
1 1. ~;g element x. Assumin g either the element x or the element y exists.
;;~ y x _ Inserts element x in the middle of the elements z and y. The element z appears before the
element y. x .
U x p _ Links the next po~ter of the elem~nt x to_ the node lying at the pth positi~n from !he element
bile traversing towards nght. In case of msuffic1ent number of nodes, the counting continue s by updating
;e existing linked list to its circular version.

Link: https://www .codeche f.com/PR ACTICE /problem s-okl/MI DLE003


. .
Program:-
\\'ftd .ff,.1 t lf" l -1...) l

. . . .
.
'~n~
'

J\'Ntt ~ck -1 tOY1p; ·cs~vu~i (dl-~ 'o4 (i4'\'lJl~· ~j;j);


~l~f 4 'I-: ~'?d Jt~d_ CJn

u \~fl_t -,. ,. l -..
l
~ f - } ne,~!'NU U.. •
/ ~-bu~t' P\ode., ~-t~p- .. J. ; .- -
tl'\~o "'" u.) <
/

f+ f , • . . t da~ J; - · : . _
h!bo ! -\-tmp 1 .,• ~p-> f\e.:1-t =NULL,
1 , /
~b1e (heaa--) do.fo J~ ~• f- \....1'~ d:li, 0 .
'
. /

{
~l'-:) ~xt: , -,

~+~f>• r
_s~L+ "'"~~ "" -hn,T' .,_ httw; /

'le.. -k.t-,n ,
w~~t. Ctbn-p -) tie'l-c ! : ~uu.} . f

{
J ' •

"tlnif' ~-l:enip --> ne1-t•


• ..
-flrn p _, 1't 1-t- ~&np I ,
J /
Sample Input and Output:

r
' .
t e, {P, o

tt, \
l \
l~4
).\':\ 1.
51
A STRUCTURES
23SC1202-DAT
ting and end are
not
points. (The star
f critical
f a lin ke d list, Find the number o l points. c
2.Given the head
o
in im a or m axim a are called criticaus elements are greater than the Jrren
al m d previo the Ct
critical points). Loc
a loca l m inim um if both next an evious elements are smaller than
A Node is calle d xt and pr
calle d a lo ca l m aximum if both ne
A Node is
Constraints 105
of elemen ts in the linked li -
st' N <
• 1< Number
5109
1. 1$Node.data ns/CRITLIST
nked-1ists/LINKLISTF/probler
actice/course/li
k: h ttps://www.codechef.corn/pr
Lin

Program:
.-S6I vt ( "4k •
head ) { .,. . ..
:,, t

' . •

. .

. . .- . .

j . I

. ..

)
"l"°'" t.,,fkta1, .
,I • ,

'
J
1_!p
r~ 3 !> ~ · s
,3
.
d Output: . , :
Sample Input an

S2
23SC 1202- DATA STRUCTURES

. n: the head node of a sorte d linke d list, whe re the data


s1° the pointer to a sorte d list wit in the node s is in asce ndin g
• h each d.1stln
• ct value 1n
• th e ongt
• •nal list. The give n head poin
g e 11 and return ter
1 Yoll arel te nodes. that the list is .
emp ty•
•def• pe e1indicating
or be n11l
J11aY
tii1111P1e d in the list 1
the first no e NUL L.
bead refers to lues and retur n head poin ting to the revis ed list 1
the 2 data va NUL L.
°
..., ve 1 f k· nk com/ chall •
enge s/del ete-d uplic ate-v alue- node s-fro m-a- sorte d-lin ked-l ist/pr
Rei••0 ·//wWV'·hac erra • oblem
Lin"'·
• Ir• hrtJ)S,

,. ., • , • •• • . \ ••• • '
progralll: -
>~"'.!~ ~" 1'e.i ti'Jt i-lod" 'Y~~ Ve. ( lj lflt iJock. 'JI {tfit )
..
"

. •.
~'\'~\~ kd llit N~ WYYe__o-l: ,;, tt t,/
'"'~ l~ (tu-r, e...Y'I t} { .
. .

J1~ ~"' \<e.d lu t ,-l~ck_

-w~~ le (rul'lne...,. -4 ne~t-) l


~.f L)on ~y -;) ne1t -> da;d C.U..-t'irui t •.4 ~~;} t •

W'rr e..n t: "'e..,, t I


I

llf~t/ I

.5
Sample Input and Outp ut:

Of p : I/ ,. I I Lf
l I
• '

53
URES
ATA STRUCT
23SC1202- D
nll !JJ ,
ou sho uld find the miniz
d list. Y
g o rde r so rte d singly linke
en a increasin to x.
2.You are giv an or equal Element With • ;
g reater th le me nts.
which is o fn e e lll(q
k e d list bu ilt on an array xti that is the index o f th ne~ e
ngly li nd ne xt i-:/:
y, there is a si is element, a
More fonnaH is the integer value in th is the last). The list is so
rted, i.e. ifne - 1,
e i e nt
integers: valu (or -1, if the current elem
st
singly linked li
i > value i.
3/B
lp ro b lem se t/problem/84
m
codeforces.co
Link: https://

Program:
. . -
.
.. . ., ' # ,.
••

-
.Sbu{t

I ' ...
•• : •
"• f
.; ,
t I
·~I

I •
. ' .
.. .. '

. '

• •"
I

. -
'
4 - • •
,

j ,t
• ' •

•, • f

t. • • ' ' .
'

. , .
t and Outp ut. •
Sample Inpu
1 •

s
-1 8o
S8 r --1 qc,
A . ' •
lt ,
g, ,
n tation of the linked list. You can cho
•go your 1·rnp.1emehould have two attributes:
.
val and next. va1 ts
. h
t ev
st
3.oe~• glY linked h st the next node.If you want to use the doubly linked list, y
5111 0
in a inter/refer~ncd~ t the previous node in the linked list. Assume all nodes in the linke
is a po • v to in ica e
·bute pre
attrl
inde"ed.
roblems desi n-linked-list descri tion

"i,, t \Ja) ) l
~bJ -> ~-th *;
P--> '-h 1 =-- \to\,/
i tA-) lfll li-ed C!J l)
t'\j tf" ~M ~Jt * (U'n Uj t Yeole t, {

~j tfr\ k,ci tfJ f * obj (_Hj tfii kd Lf..! 1 >Y10 ,10L (.s'-11.~ o/ (_m~t?l\~Lf.J t))'
/

()'\J (f.-, lt-ed lfJ t 'lO ( LrYtj Cf11 kcJ lfJ f


obj -:;) "'~"" t :: N utu
I
Obj-} ~th +,;
- , .. ~. '
m1 lf>,-. )-e~ t
p.,, Lrnj lest "')~ >-YQfbe
( .!f2-e o-1 (
tfr1 ~t C':) [ff) kd. t1..1t -1 •obj , f"' f
lfh ><~ct tf..\l)),
f·.:_'> \Jof .; Vo.l •
f+
J
(t.bf 4--~ ~-th .t.f~t'I-, 11: f'ndo I- -I) { ,I

~t.fu.,n -r . f~ t1eit t _: ~uu.. ,·


J
Obj -
Ab,,,J 4
v ne .,._ t',I,
rjl)i- r.!.~.
Sample Input and Output: / tJ-9 h'r )e

obj .: ney{. l
.p ,I
,t-, I

,I

• . \

• l '

e, Jp : u l\. , "'o l t I n u l t] 55
23SC1202- DATA STRUCTURES

the nod e at which the


ed-lists headA and headB, return
4.Given the heads of two singly link
intersection at all, return null.
intersect. If the two linked lists have no
lists begin to intersect at node cl:
For example, the following two linked

.'
A. ,. •'

. . . ..
••. 'I

. ,

.. .
B: • >

c~ e:.
e generated suc h that ther e are. no ~yc les anywhe~e in. the entir~ link:~d ~~
The test cases-ar
of-two-linked-Jists/description/
~in k: _https:/~etcode.comfproblems/inters.ection-
.. . .
. ¼ipt+ ~d A; •• ,,
Pro gram: '
.' .' ..
- Utnp B = hea d .- • •
Jfn.uf I
I

~11)
fAJh fl e Ldf ff :>o ) l
. .
p6 :_>'
·,

:'_ ,-ternp iA (
ne-,t·. . . , : •
w~ lt l !tmr. A!·~ ,..rvtt) (
./
' ,~...
do./.-1 • ,. • J • .. •.

r~.J#t . '+J , ·~ . . , -.
.'. 4
.,. • •
'"

l ~ • -..
4 • ...
•• •

, ,

J . ;

~ernp_! _le~p(I. ->~-fi:utl ,,


. . .
'

p· 8 -~- f tmp, 13 -) ne) l.l' .....


~" l d~.f( - • I
- fj-< >, -- k>n I
. .
Sample Input and Output: ' ' ' ,

Ilp : fr1tt r .St t l.fd I -::


I lfs{ A > Li, q, , , )._, l.lJ , ih ( e; '[_S-;i..,·c.i J·, A,; ••
..
' .
Jn~.Stcitcf ol
56
~ -

23SC1202- DATA STRUCTURES

h middle of a given linked list using recursion


s. Find t. e ly linked list, find middle of the linked list. For example, if given linked list is 1->2->3->4->5 then
a·ven
1 a sing
hould be 3.
output 8 even nodes, then there would be two middle nodes, we need to print second middle element. For
If the~ ar~f given linked list is 1->2->3->4->S->6 then output should be 4.
1
exaJllP e, rt function should add nodes in the linked list.
Not~s;irst line No. of Node n in linked list. Second Line Should be n Space Seprated Data of LinkedList.
INP :Output Should be middle element of Linked.
output. Link: https://www.hackerrank.com/contests/ds-week3/challenges/find-the-middle-of-a-given-linked-list-
Y§ing-recursion
program: -
Yrue1i (..S·hul t ~ e *"' r Y\~ , f" l rw.,)
t_l'hut4 ~de ..,_ ) ,v,o)1ot
',,,
I

1'~Wl'l~d e.. -~ f'e'f.f :, N ULt- ; " : '


.'
e
f .( (h.('od :: =- Nuu,) l ..
I J ,

11\aid '14..w ~d~, • • I


I

-} ne..,. t•, . . . , .. s

_) ' C

~t • t-fiddle .S-rr~t., f

..ltvut4 .,. .Slew heod , •
,f

~t4 ~t 31' fut h~)


,I

"" ii? 1t ( 1ru f l : i"tutl-


"' t no f C ,.J uu.,) l
: .1 tt,w Y\t'lc •
)

SarnpJe Input and Outpu~: 4GJ 1o.J --} Ylev~


j
s
~,p: 3
57
23SC1202- DATA STRUCTURES

6.Starting from the second node delete all alternate


nodes.
Given a Singly Linked List, starting from the secon
d node delete all alternate nodes of it.
Sample Input
10 20 30 40 50 None
Sample Output
10 30 SO None

Link: https://www.hackerrank.com/contests/ds-wee
k3/challenges/starting-from-the-second-node-delete.
alternate-nodes-

..
Program: - I t \• .. . ' '\ ,. \ .
\b16 de\o{t (St!ott
• . .f ~eod) . I 0

\ '
~t ~at d !. NOLL)

I ' • I l

.•

f..,e.... \I -'> )'\ e., t

~e (~d t),
/

P-Yi \) ~'iQ y\Q 1,


) I

/
. ...
. .. '·

, . ..•

. "
.. ' I

ntu, >'Wde -\ fle >tt "; Lheo.d


-~ 'Ye4).
/ . '. '.
Ch~d - 'f<l f- ) .. l'lew l'\Ode '
.l \ ,. ...
I '
Sample Input and Output: } •I

(For Evaluato(s use only) .


' ( '
I , . .. . : ,
I
'

• ·'

Comment of the Evaluator(if Any} Evaluator's Observation:!


• a

Mark
,
s Secured: . outo.f
\

,
Full Name of the Evaluator:
I

Signature of the Evaluator Date of Evaluation:


.

58

You might also like