Practical Session #3 - Adts: Array, Queue, Stack, Linked List
Practical Session #3 - Adts: Array, Queue, Stack, Linked List
Linked List
Basic Data Structures and Abstract Data Types
ADT
Abstract Data Type
A collection of data-storing entities it! operations to create, access,
c!ange, etc"
#ector$Array%
A linear se&uence of ele'ents t!at supports access to its ele'ents by
t!eir inde(es"
A )ector ADT supports t!e folloing basic 'et!ods:
ele'entAt$i% * returns t!e ele'ent at inde( i
replaceAt$i, ele'ent% * replace and return t!e ele'ent t!at in
inde( + it! ne gi)en ele'ent"
insertAt$i, ele'ent% * insert ne ele'ent at inde( i
re'o)eAt$i% * re'o)e t!e ele'ent in inde( i
si,e$% * returns t!e si,e of t!e )ector
is-'pty$% - returns true if t!e )ector is e'pty
Queue
.+./ - .irst +n .irst /ut
ADT t!at supports t!e folloing operations:
-n&ueue - insert a ne ele'ent at t!e tail of t!e &ueue
De&ueue - re'o)e an ele'ent fro' t!e !ead of t!e &ueue
is-'pty - returns true if t!e &ueue is e'pty
Stack
L+./ - Last +n .irst /ut
ADT t!at supports t!e folloing operations:
Pus! - add an ele'ent to t!e !ead of t!e stack
Pop - re'o)e an ele'ent fro' t!e !ead of t!e stack
is-'pty * returns true if t!e stack is e'pty
Linked List
A data structure it! linear access to t!e ele'ents in it"
-ac! ele'ent !as a pointer to t!e ne(t ele'ent in t!e list
Doubly-Linked
List
A data structure it! linear access to t!e ele'ents in it"
-ac! ele'ent !as 0 pointers, one to t!e ne(t ele'ent in t!e list and t!e
ot!er to t!e pre)ious ele'ent
Question 1
S is a set of at 'ost n ele'ents"
-ac! ele'ent !as a uni&ue key and is in t!e range 12"""n-34, n is gi)en"
.ind a data structure t!at supports t!e folloing operations in t!e gi)en ti'e:
/peration Ti'e
+nit$n% +nitiali,e S $S is an e'pty set at t!e begining% /$n%
is-le'ent$k% 5!eck if S !as an ele'ent !ose key e&uals to k /$3%
+nsert$(% Add ele'ent to S /$3%
6e'o)e$k% 6e'o)e ele'ent !ose key e&uals to k /$3%
is-'pty$% 5!eck if S is e'pty /$3%
!asAll
5!eck if S contains all t!e ele'ents !ose keys are
in t!e range 12"""n-34
/$3%
Solution: (Direct Addressing) :
3" Array A13""n4 of pointers to ele'ents"
if A1k4 77 null, t!e ele'ent !ose key is k is not in S"
0" count 7 nu'ber of ele'ents in S
+nit $n%
for $i72 8 i9n 8 i::%
A1i4 7 null8
count 7 28
is-le'ent $k%
return $A1k4 ;7 null%8
+nsert $(%
A1("key4 7 (8
count::8
6e'o)e $k%
A1k4 7 null8
count--8
is-'pty $%
return $count 77 2%8
!asAll $%
return $count 77 n%8
Question 2
Suggest 0 ays to i'ple'ent a Queue using 0 Stacks"
Solution :
T!e &ueue<stack is abstract, 'eaning t!e i'ple'entation isn=t knon, only t!e interface"
+n order to i'ple'ent a &ueue e need to i'ple'ent t!e folloing 'et!ods:
-n&ueue$(%
De&ueue$%
is-'pty$%
e ill use stack A in order to !old t!e &ueue=s ele'ents and stack > as a te'porary
stack"
-n&ueue $(% ?
'o)e-le'ents$A,>%
A"Pus!$(%
'o)e-le'ents$>,A%
@
De&ueue $ % ?
ele'ent 7 A"PoP$%
return ele'ent
@
is-'pty $ % ?
return A"is-'pty$%
@
'o)e-le'ents$A,B% ?
!ile $; A"is-'pty$%%?
te'p 7 A"Pop$%
B"Pus!$te'p%
@
@
Anot!er ay to i'ple'ent a &ueue using 0 stacks:
-n&ueue $(% ?
'o)e-le'ents$A,>%
A"Pus!$%
@
De&ueue $ % ?
'o)e-le'ents$>,A%
ele'ent 7 A"Pop
return ele'ent
@
is-'pty $ % ?
return $ A"is-'pty$% CC >"is-'pty$%%
@
In a very similar ay you can implement stac! using 2 "ueues#
Question $:
L3 and L0 are to linked lists !ic! !a)e a co''on part of si,e k"
T!e lengt! of L3, L0 until t!e 3
st
co''on link is n,' respecti)ely"
Suggest a ay to find t!e 3
st
co''on link at /$n:':k%"
Solution :
3" find t!e lengt! of L3 and L0
0" +n t!e longer list, find t!e $L3"lengt!-L0"legt!%t! ele'ent"
3" Tra)erse bot! lists step by step toget!er" +n t!e s!orter list start fro' t!e first link"
+n t!e longer list start fro' t!e link you found in 0" Stop !en you get a co''on
link
ti'e co'ple(ity:
finding t!e lengt! of L3: /$n:k%
finding t!e lengt! of L0: /$':k%
finding t!e co''on link: /$':n%
total: /$n:':k%
Question %:
L is a singly linked list"
Suggest a ay to tra)erse forard and backard L ,starting fro' t!e first ele'ent"
Do)ing fro' one link to !is ne(t or pre)ious link s!ould be perfor'ed at /$3%"
Bou can use only /$3% e(tra 'e'ory"
Solution:
5on)erting t!e list to a soubly-linked list can do t!e trick, but it is not alloed $ill cost
/$n% e(tra 'e'ory%"
Ee ill use to pointers:
current * t!e current link e ant to tra)erse fro'"
pre) * t!e pre)ious link in t!e tra)ersal"
As e tra)erse forard, all t!e links before current ill be linked backard"
As e tra)erse backard, all t!e links after current ill be linked forard as usual"
init$%
current7!ead
pre)7null
'o)e.orard$%
te'p 7 pre)
pre)7current
curent7current"ne(t
pre)"ne(t7te'p
'o)e>ackard$%
te'p7current
current7pre)
pre)7pre)"ne(t
current"ne(t7te'p
Fotice t!e sy''etry beteen 'o)e>ackard$% to 'o)e.orard$%
$current and pre) c!ange parts%"
Question &:
A is a boolean $3 or 2 )alues% 'atri( of si,e nGn"
.ind a data structure t!at supports t!e folloing operations in t!e gi)en ti'e:
/peration Ti'e
init$n% +nitiali,e A it! t!e )alue 3 /$nGn%
flip$i,H% A1i,H47;A1i,H4 /$3%
!as6o/f3 6eturn true iff A !as a ro t!at contains only 3-s /$3%
!as6o/f2 6eturn true iff A !as a ro t!at contains only 2-s /$3%
Solution:
Ee ill use t!e uses t!e folloing data structures:
A1n41n4 * t!e 'atri(
Su'1n4 * su' of t!e ros in A" Su'1i4 7 t!e su' of t!e ro i in A"
count3 7 !o 'any cells in Su' contains n
count2 7 !o 'any cells in Su' contains 2
init$%
fill A it! 3Is
fill Su' it! n"
count37n
count272
flip$i,H%
if $A1i41H4772% ?
if $Su'1i472%
count2- -
Su'1i4::
if $Su'1i47n%
count3::
A1i41H473
@
else ?
if $Su'1i47n%
count3- -
Su'1i4--
if $Su'1i472%
count2::
A1i41H472
@
!as6o/f3$%
return count3J2
!as6o/f2$%
return count2J2
Question ':
Suggest a ay to i'ple'ent an array of integers so t!at initiali,ing it it! +F+TK#AL
ill take /$3% ti'e"
6e'e'ber t!at reading A1i4 or riting to A1i4 takes /$3% ti'e"
Solution:
+n order to i'ple'ent an initiali,ation of an array A of si,e n in /$3% ti'e, e ill
create a ne ADT called s'artArray"
T!e s'artArray ADT uses t!e folloing data structures:
A - t!e array of )alues
top * counts t!e nu'ber of defined )alues in A
+F+TK#AL* t!e )alue to initiate t!e array it!
5 * Array of defined and undefined inde(es
512""top-34 * defined inde(es in A
51top""n-34 * undefined inde(es in A $+F+TK#AL%
> * array of pointers to t!e defined inde(es in 5
>1k4 7 t!e inde( of k in t!e array 5
T!e s'artArray ADT supports t!e folloing 'et!ods:
ne: 1Arr 7 ne int1n44
FeS'artArray$n%
A 7 ne int1n4
> 7 ne int1n4
5 7 ne int1n4
top 7 2
init$)al%
+F+TK#AL 7 )al
T!e folloing procedure returns true if a gi)en inde( LiI is not initiali,ed"
notKdefined$i%
return ;$$>1i4 9 top% and 51>1i44 7 i%%
Fote t!at >1i4 could contain a M,e)elN )alue"
>ut for e)ery >1i49 top , 51>1i44 contain true )alues $i"e non *M,e)elN )alues%
(ead(i): 1)al O Arr1i44
)rite(i*val) : 1Arr1i4 O )al4
6ead$i%
if $notKdefined$i%%
return +F+TK#AL
else
return A1i4
Erite$i,)al%
if $notKdefined$i%%
51top4 7 i
>1i4 7 top
top::
A1i4 O )al
+,ample:
( 7 ne s'arArray1n4
("init$2%
("rite$0,02%
("rite$P,P2%
("rite$Q,Q2%
a 7 ("read$P% JJ t!e )alue of a is P2
b 7 ("read$3% JJ t!e )alue of b is 2
3
0
3
2
Q Q2
P P2
3
0 02
3
2
A
Q 0
P 3
3
0 2
3
2
>
Q
P
3
0 Q
3 P
2 0
5
not set
top
set