A RRAYS IN J AVA : T HE B ASICS Section 6.0 AP Computer Science.
E LEMENTARY D ATA STRUCTURES. A RRAYS Sequence of elements of similar type. Elements are selected by...
-
Upload
anthony-preston -
Category
Documents
-
view
214 -
download
0
Transcript of E LEMENTARY D ATA STRUCTURES. A RRAYS Sequence of elements of similar type. Elements are selected by...
ELEMENTARY DATA STRUCTURES
ARRAYS
RECORDS
Sequence of elements having diverse types and sizes. Elements of a record are called fields.
name
address
phone_number
directory_entry: record type name: string length = 20 address: string length = 20 phone_number: integer
callee: directory_entryphone_number(callee)← 751489or phone_number[callee]← 751489
?????
?????
751489
callee
directory_entry
RECORDS WITHIN RECORDS address_type: record type street_number:integer
street_name:string length=16 directory_entry: record type name: string length = 20 address: address_type phone_number: integer customer:directory_entry Now we can refer to an entire address as in: address(customer) street_name(address(customer) address(payer) ← address(customer)
RECORDS WITHIN RECORDS
There can be record types in which one or more of the fields are themselves records of the same type.
directory_entry: record type name: string length = 20 address: address_type phone_number: integer subsidiary_listing:directory_entry listing : directory_entry A variable declared like this appears to require infinite
storage as shown below:
name
address
phone_number
subsidiary_listing name
address
phone_number
.
.
.
Storage layout for a directory_entry requiring infinite storage
RECORDS AND POINTERS To avoid the need for infinite storage, we can use pointer
variables rather than record variables. A pointer variable does not itself contain the fields of a record
; instead it specifies the location of the actual record value. Thus, declarations of the following form :
variable_name:record_type will cause space to be allotted only for a pointer, not for a
record. To create the record values themselves we will use the
primitive getcell such as listing ← getcell, address(listing) ←getcell subsdiary_listing (listing)←getcell
Example of record variables implemented as pointers to records
?
?
?
listing
directory_entry
address
directory_entry
directory_entry
IMPLEMENTATIONS IN MEMORY The memory of a computer composed of
a sequence of words. Each word is referred to by its unique
address. The address of the first word is normally 0 A memory of 2k words to have addresses
that are k bits long.
We use the notation: loc(variable), to represent the address to be used for the variable.
loc(x), returns the address of x in memory.
0
1
2
3....
.
.
.
.
2k-1
IMPLEMENTATIONS IN MEMORY(ARRAYS)
Arrays are arranged in order in adjacent memory locations.
A : array [l : u] of item (where item is w words long). A[l] is stored, beginning in location L. A[l+1] is stored in location L+w. A[l] is stored in location L+2w. A total of (u – l + 1)w words of memory are used.
In this implementation there is a simple relationship between an index i and loc(A[ i ]) , l ≤ i ≤ u:
loc(A[ i ])=loc(A[ l ])+(i-l)w
Diagram of an array A[l:u] as arranged in memory
A[l]: L...
A[l+1]: L+w...
A[l+2]: L+2w
.
.
.
.
.
.
A[u]: L+(u-l)
IMPLEMENTATIONS IN MEMORY(TWO DIMENSIONAL ARRAYS)
To implement a n x m array,
Consider it as an array B[1], B[2],….,B[n] in which B[i] in turn an array of m elements consisting of the i th row of the matrix.
A[1,1] A[1,2] ……… A[1,m]
A[2,1] A[2,2] ……… A[2,m]
. ..
. ..
. ..
. ..
A[n,1] A[n,2] ……… A[n,m]
Thus the number of words needed for an element B[i] = mw.
Since, w = number of words needed for each element A[i,j].
Thus location of B[i]: loc(B[i]) = loc(B[1])+(i - 1)mw loc(A[i,j]) = loc(B[i]) + (j - 1)w = loc(B[1]) + (i -1)mw + (j - 1)w = loc(B[1]) + [(i-1)m +(j - 1)]w = loc(A[1,1]) + [(i - 1)m+(j - 1)]w
IMPLEMENTATIONS IN MEMORY(RECORDS)
A pointer value can be implemented simply as the address of the corresponding record.
A record is just a consecutive block of words. Suppose we declare a record type as, type_name: record type field_name1 : item1
field_name2 : item2
………………………………
field_namen : itemn
field_name1 len1
field_name2 len2
field_name3 len3
field_namej-1 lenj-1
field_namej
leni
1
1
j
iilen
P:
loc(P)
1
1
j
iilenP
1
1
))(_(j
iij lenPPnamefieldloc