69 Views

Download Presentation
##### 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. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Data Structures**Arrays**Arrays**• An array is a homogeneous aggregate of data elements in which an individual element is identified by its position in the aggregate relative to the first element. A [ ][ ][ ][ G ][ … ][ ][ ] A[G] -> Access the Gth element of the array. • The operation that selects a component from the array is called scripting or indexing. Array_Name[ Subscript_Value ] • Selection operation can be though as a mapping from the array name and a set of subscript values to an element of the aggregate.**Arrays: Array Descriptor**Array Descriptor in Stack**Arrays: L-Value (L-Address)**• Let us assume that the 1st element of the array “A” begins at location a. L-Value(A[ N ]) = a + ( N – LB ) * E L-Value(A[ N ]) = a – LB * E + N * E K = a – LB * E and is a constant value. L-Value(A[ N ]) = K + N * E When E = 1 and LB = 0, then: L-Value(A[ N ]) = a + N • B: Array[ 3 … 10 ] of INT a = 100 L-Value(B[ 4 ]) = 100 – 3 + 4 = 101 LB = 3 E = 1 LB ≤ N ≤ UB**Arrays: Virtual Origin (VO)**• So far we know: L-Value(A[ N ]) = (a – LB * E) + ( N * E ) • And for A[ 0 ]: L-Value(A[ 0 ]) = (a – LB * E) + ( 0 * E ) • Sometimes the lower bound of the array could be greater than zero. This address is called the virtual origin.**Arrays: Finding the Virtual Origin**• On creation of vector storage, allocate “N” ( UB – LB + 1) components of the vector of size E and the descriptor of size “D”. • Compute the Virtual Origin:VO = a – LB * E Example: VO = 100 – 3 * 1 = 97 • On accessing an array component (assuming LB £ N £ UB):L-Value(A[ N ]) = VO + N * E**Arrays: Finding the Virtual Origin**• Example: A[ 3 … N] Assuming: a = 100 E = 1 VO = 97 (See previous slide, step 2) L-Value(A[ 4 ]) = VO + N * E = 97 + 4 * 1 = 101**Ordering:**Example: M = 3, 4, 7, 6, 2, 5, 1, 3, 8 Row major order: M: Column major order: M: Arrays: Multidimensional Arrays**Arrays: Multidimensional Arrays**• Storage • Location of A[ N , J ] is given by: L-Value( A[ N , J ] ) = a + ( N – LB1 ) * S + ( J – LB2 ) * E a = Base Address S = Length of a Row ( UB2 – LB2 + 1) * E VO = a – LB1 * S – LB2 * E • Example: L-Value( A[ N , J ]) = VO + N * S + J * E L-Value( A[ 2 , 3 ]) = ( 2 * row length) + 3**Arrays: Multidimensional Arrays**• Slices • Slices of a 2 dimensional array (matrix) are treated as an array. • L-Value( A[ N , J ] ) = VO + N * 3 + J * 1 VO = a – LB1 * S – LB2 * E = a – 4 Assuming that a = 100: VO = 96 L-Value( A[ 2 , 2 ] ) = VO + N * 3 + 2 * 1 = 96 + 2 * 3 + 2 * 1 = 96 + 6 + 2 = 104**Data Structures**Dynamic Arrays**Dynamic Arrays**A[ ] ≡ A[ ] ≡ A[ ][ ] [ ] ≡ Dynamic Arrays x**Dynamic Arrays (Continued)**New int [100] int finalMark – allocating space in memory (a way to access that memory location) finalMark[ ] = new int [100] int marks[ ][ ] = new int [3][2] finalMark pointer arithmetic**DESCRIPTOR**DESCRIPTOR DESCRIPTOR ARRAY DESCRIPTION y x AR Dynamic Arrays (Continued) List [0] List [1] List [2] List [3] 1000i x 8 P 1000 1008 1016 P + K ≡ &List[K] (pointer arithmetic) y x AR**Data Structures**Records**A record is a heterogeneous data structure composed of a**fixed number of components of different types • The record elements are referenced by names (identifiers) • Records were introduced by COBOL Example: 01 Employee – Name 02 Employee – Name 05 First Name Picture is x(20) 05 Middle Name Picture is x(10) 05 Last Name Picture is x(20) 02 Hourly Rate Picture is 99v99 Name Hourly Rate First Middle Last**ID**AGE SALARY DEPT Record Example Let us assume we have a record structure called employee with 4 fields (ID, AGE, SALARY, DEPT) To address a component of the array, the selector operator “.” is used. Example: Employee.ID; Employee.DEPT; int int int int**L-Address (value) (R.I.)**L-Address (R.I.) = α + ∑ (size of R.J.) We need a record descriptor I = 1 Field J J = 1 α R.I. Field**α**RECORD NAME TYPE OFFSET NAME TYPE OFFSET NAME TYPE OFFSET Record Description TYPE { RECORD During Execution Time NAME FIELD 1 TYPE OFFSET OFFSET { NAME L-value (R.I) = α + KI TYPE FIELD 2 OFFSET . . . NAME { TYPE FIELD N OFFSET A ID AGE DEPT SALARY**Record Description (Continued)**β A[I] LB – Lower Bound UB – Upper Bound A[I].ID α RECORD DESCRIPTOR ARRAY DESCRIPTOR { Y X DESCRIPTOR AR UB LB (offset is the same for each element) → use one record descriptor (displacement)