Sie sind auf Seite 1von 3

Practical Session #3 - ADTs: Array, Queue, Stack,

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( i 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 ne ele'ent at t!e tail of t!e &ueue
De&ueue - re'o)e 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 ele'ent to t!e !ead of t!e stack
Pop - re'o)e 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 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 beginning% /$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%
Question 2
Suggest 0 ays to i'ple'ent a Queue using 0 Stacks"
Question 3:
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 node is n, ' respecti)ely"
Suggest a ay to find t!e 3
st
co''on node at /$n7'7k%"
$k,n,' are )ariables, not gi)en%
Question 4:
L is a singly linked list" Starting fro' t!e first ele'ent, suggest ay to tra)erse forard
and backard fro' one ele'ent to !is ne(t or pre)ious ele'ent at /$3%" 8ou can use
only /$3% e(tra 'e'ory"
Question 5:
A is a 9oolean $3 or 2 )alues% 'atri( of si+e n:n"
-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 /$n
0
%
/peration Ti'e
flip$i,;% A1i,;4<=A1i,;4 /$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%
Question 6:
Suggest a ay to i'ple'ent an array of integers so t!at assign'ent of so'e )alue
.>.T?#AL to all cells of an array ill take /$3% ti'e"
6e'e'ber t!at reading A1i4 or riting to A1i4 takes /$3% ti'e"

Das könnte Ihnen auch gefallen