Вы находитесь на странице: 1из 7

CS 211 Data Structures and Algorithms

by Jayson G. Mauricio

CS 211: Data Structures and Algorithms

LECTURE 3: Types of Data Structures

by Jayson G. Mauricio Our Lady of Fatima University Antipolo Campus

Lecture 3: Types of Data Structures

CS 211 Data Structures and Algorithms

by Jayson G. Mauricio

LECTURE 2: TYPES OF DATA STRUCTURES Types of Data Structure 1. Linear the elements form a sequence e.g. array, linked lists 2. Non-linear structured e.g. trees, graphs

Data Types - composed of domain data elements Data Item single unit of information Types of Data Item 1. Group Item data item that can be divided into sub-items. e.g. name (first name, middle name, last name), address (street, town, city, country), date of birth (month, day, year), time (hour, minute, seconds), etc. Elementary Item - data item that cannot be divide into sub-items. e.g. age, gender, etc.

2.

Classification of Data Types A. Elementary data types or Simple data types these are the basic data types the value of one of these components is atomic, that is it consists of a single entity and could not be divided. e.g. int, float, char in C. 1. 2. Primitive data types - enumerated data types Standard primitive built-in data types e.g. int, char, float

B.

Structured data types collection of complex number of information. 1. 2. 3. 4. Strings is an ordered sequence of characters that increases and decreases dynamically. Lists ordered sequence of components, which may themselves be lists. Arrays fixed-size, ordered collection of data elements all of the same type. Records also called the hierarchical or structured type.

Lecture 3: Types of Data Structures

CS 211 Data Structures and Algorithms

by Jayson G. Mauricio

- is an ordered collection of data elements that are not necessarily of the same type.

ARRAYS One of the most commonly used data structures. Components of an array 1. array name collective name of an array. 2. index type set of subscripts that are used to differentiate one element from another. 3. base type data type of array element. Types of array 1. One-dimensional or single dimensional array - also terms a vector, this type of array simply refers to a specific number of consecutive memory locations. 2. Multi-dimensional array the position of data element must be specified by giving coordinates, typically the row and column coordinates. Operations of Array 1. Storage assign a value to a particular array element 2. Extraction getting a value from an array element ONE DIMENSIONAL ARRAY also called the linear array it is a list of finite number of N elements of homogenous data elements that: a) the element of the array are referenced respectively by an index, set consisting of N consecutive numbers. b) The elements of the array are stored respectively in successive memory locations. Syntax: Example: <base_type> <array_name>[<index>]; int x[4];

To get the Total number of elements of an array: We use formula: NE = UB + LB + 1 Where: NE number of elements UB upper bound (highest index of an array) LB lower bound (lowest index of an array) To compute for the address of an element (memory location)

Lecture 3: Types of Data Structures

CS 211 Data Structures and Algorithms

by Jayson G. Mauricio

Loc[k] = base + w(k-LB) Where: k- index of a specific element base starting memory address w - element size (words per memory cell/interval) LB lower bound (lowest index of an array) Sample Problems 1. Consider an array grade[8], w=2, base =100. Look for the address of grade[5] and NE Given: UB=7 LB = 0 w=2 base = 100 Find: Loc[5] & NE Solution: Loc[k] = base + w(k-LB) Loc[5] = 100 + 2(5-0) = 100 + 2(5) = 110 2. NE = UB LB + 1 =70+1 =8 Memory Mapping Element Address 0 1 2 3 4 5 6 100 - base 102 104 106 108 110 112

An array has an index of (-2..5) at the starting address of 200. It it has 3 words per memory cell, determine Loc[-1], Loc[3], NE. Given: LB = -2 NE = UB LB + 1 UB = 5 = 5 (-2) + 1 base = 200 =7+1 w =3 =8 Find: Loc[-1], Loc[3] & NE Solution: Loc[k] = base + w(k-LB) Loc[-1]= 200 + 3(-1 (-2)) = 200 + 3(1) = 203 Loc[k] = base + w(k-LB) Loc[3]= 200 + 3(3 (-2)) = 200 + 3(5) = 215 Element -2 -1 0 1 2 3 4 5 Address 200 -base 203 206 209 212 215 218 220

3.

The starting address of an array is 872. 3 is the highest index of the array. If it has 9 elements and w=4, find which element has the location of 892.

Lecture 3: Types of Data Structures

CS 211 Data Structures and Algorithms

by Jayson G. Mauricio

Given: base = 872 UB= 3 NE= 9 w=4 Find: k Solution: Loc[k] = base + w(k-LB) k= Loc[k] base + LB w

k= Loc[k] base + LB w k= 892 872 + (-5) 4 =0 Memory Mapping Element Address -5 872 - base -4 876 -3 880 -2 884 -1 888 0 892 1 896 2 900 3 904

4.

NE = UB LB + 1 LB = UB NE + 1 =39+1 =49 = -5 The third element of an array is 4 whose location is 558. If w=5 and there are 7 elements in the array, compute for the base and Loc[0]. Given: Loc[-4] = 558 w=5 NE = 7 LB = -6 1st element Find: base & Loc[0] Solution: Loc[k] = base + w(k-LB) Base = Loc[k] w(k-LB) 558 = base + 5(-4 (-6)) 558 = base + 10 base = 558 10 base = 548 Loc[k] = base + w(k-LB) Loc[0] = 548 + 5(0 (-6)) = 548 + 30 = 578 Memory Mapping Element Address -6 548 - base -5 553 -4 558 -3 563 -2 568 -1 573 0 578

5.

Consider an array of 12 elements. If the 1st element of the array starts at memory address 997 and its highest index is 7 whose location is 1074, look for the words per memory cell. Given: NE =12 base = 997 UB = 7 Loc[7] = 1074 Find: w

Lecture 3: Types of Data Structures

CS 211 Data Structures and Algorithms

by Jayson G. Mauricio

Solution: NE = UB LB + 1 LB = UB NE + 1 = 7 12 + 1 = 8 12 = -4 Loc[k] = base + w(k-Lb) 1074 = 997 + w(7- (-4)) 1074 997 = w(11) 1074 997 = w(11) 11 11 w = 1074 997 11 w=7 TWO-DIMENSIONAL ARRAY -

Memory Mapping Element Address -4 997 -3 1004 -2 1011 -1 1018 0 1025 1 1032 2 1039 3 1046 4 1053 5 1060 6 1067 7 1074

Collection of M x N elements such that each element in the array is referenced by a pair of integer such as j,k called the subscript. Also called matrix in mathematics and tables in business applications therefore they are considered to be matrix array Has elements that form a rectangular array

Syntax: <base_type> <array_name>[<index>] [<index>]; e.g. int x[3][4]; 3 x 4 array in row major - column varies faster than row
1 2 3 1 1,1 1,2 1,3 2 2,1 2,2 3,2 3 3,1 3,2 3,3 4 4,1 4,2 4,3

Illustration: 3 x 4 array in column major - row varies faster than column


1 2 3 1 1,1 2,1 3,1 2 1,2 2,2 3,2 3 1,3 2,3 3,3 4 1,4 2,4 3,4

To determine the Total number of elements: NE = M * N Where: NE = number of elements M = UB1 LB1 + 1 N = UB2 LB2 + 1 To compute for the address of an element (memory location) Row major: Loc[j,k] = base + w[ N(j - LB1) + (k - LB2) ] Column major: Loc[j,k] = base + w[ M(k - LB2) + (j - LB1) ] Where:

Lecture 3: Types of Data Structures

CS 211 Data Structures and Algorithms

by Jayson G. Mauricio

j,k index of an element in the array Sample problems 1. Given an array with (2..5, -3..6) of integer. Base=100, w=4. Find the Loc[4,2] using column major. Given: UB1 = 5 LB1=2 UB2 = -3 LB2 = 6 base = 100 w=4 Find: Loc[4,-2] Solution: N = UB2 LB2 + 1 = 6- (-3) + 1 = 10 2. An 25 x 4 matrix array has a base of 200 and 3 words per memory cell. Determine the Loc[10,2] using column major. Given: Base = 200 w= 3 M = 25 N=4 Find: Loc[10,2] Solution: NE = M * N = 25 * 4 = 200 Loc[j,k] = base + w[ M(k - LB2) + (j - LB1) ] Loc[10,2] = 200 + 3[25(2-1) + (10-1)] = 200 + 3(25+9) = 200 + 3(34) = 200 +102 = 302 Loc[j,k] = base +w[N(j-LB1) + (k-LB2)] Loc[4,-2] = 100 + 4[10(4-2) + (-2 (-3))] = 100 + 4[10(4) + 1] = 100 + 4(21) = 100 + 84 = 184

Lecture 3: Types of Data Structures

Вам также может понравиться