1 / 15

Elementary Data structures

Elementary Data structures. Arrays. Sequence of elements of similar type. Elements are selected by integer subscripts. For example, signs: array [37:239] of integer. records. Sequence of elements having diverse types and sizes.

barth
Download Presentation

Elementary Data structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Elementary Data structures

  2. Arrays • 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. directory_entry 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 callee

  4. 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)

  5. 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:

  6. Storage layout for a directory_entry requiring infinite storage

  7. 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 getcellsuch 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 ? 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 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.

  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

  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.

  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_name1 : item1 field_name2 : item2 ……………………………… field_namen : itemn

  15. P: loc(P)

More Related