Presentation is loading. Please wait.

Presentation is loading. Please wait.

E LEMENTARY D ATA STRUCTURES. A RRAYS Sequence of elements of similar type. Elements are selected by integer subscripts. For example, signs: array[37:239]

Similar presentations


Presentation on theme: "E LEMENTARY D ATA STRUCTURES. A RRAYS Sequence of elements of similar type. Elements are selected by integer subscripts. For example, signs: array[37:239]"— Presentation transcript:

1 E LEMENTARY D ATA STRUCTURES

2 A RRAYS Sequence of elements of similar type. Elements are selected by integer subscripts. For example, signs: array[37:239] of integer.

3 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_entry phone_number(callee) 751489 or phone_number[callee] 751489 ????? 751489 callee directory_entry

4 R ECORDS 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)

5 R ECORDS 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:

6 name address phone_number subsidiary_listingname address phone_number... Storage layout for a directory_entry requiring infinite storage

7 R ECORDS 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

8 Example of record variables implemented as pointers to records ? ? ? listing directory_entry address directory_entry

9 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 2 k 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................ 2 k -1

10 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.

11 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)

12 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]

13 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

14 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_name 1 : item 1 field_name 2 : item 2 ……………………………… field_name n : item n

15 field_name 1 len 1 field_name 2 len 2 field_name 3 len 3 field_name j-1 len j-1 field_name j len i P:P: loc(P)


Download ppt "E LEMENTARY D ATA STRUCTURES. A RRAYS Sequence of elements of similar type. Elements are selected by integer subscripts. For example, signs: array[37:239]"

Similar presentations


Ads by Google