Cs 211 lecture 03 types of data structures

717 views

Published on

Our

Published in: Education, Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
717
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Cs 211 lecture 03 types of data structures

  1. 1. 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 sk_jam28@yahoo.com 0906-1531294Lecture 3: Types of Data Structures 1
  2. 2. CS 211 Data Structures and Algorithms by Jayson G. Mauricio LECTURE 2: TYPES OF DATA STRUCTURESTypes of Data Structure1. Linear – the elements form a sequence e.g. array, linked lists2. Non-linear – structured e.g. trees, graphsData Types - composed of domain data elementsData Item – single unit of informationTypes of Data Item1. 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.2. Elementary Item - data item that cannot be divide into sub-items. e.g. age, gender, etc.Classification of Data TypesA. 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. Primitive data types - enumerated data types 2. Standard primitive – built-in data types e.g. int, char, floatB. Structured data types – collection of complex number of information.Lecture 3: Types of Data Structures 2
  3. 3. CS 211 Data Structures and Algorithms by Jayson G. Mauricio 1. Strings – is an ordered sequence of characters that increases and decreases dynamically. 2. Lists – ordered sequence of components, which may themselves be lists. 3. Arrays – fixed-size, ordered collection of data elements all of the same type. 4. Records – also called the hierarchical or structured type. - is an ordered collection of data elements that are not necessarily of the same type.ARRAYSOne of the most commonly used data structures.Components of an array1. 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 array1. 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 Array1. Storage – assign a value to a particular array element2. Extraction – getting a value from an array elementONE – 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: <base_type> <array_name>[<index>]; Example: int x[4]; To get the Total number of elements of an array:Lecture 3: Types of Data Structures 3
  4. 4. CS 211 Data Structures and Algorithms by Jayson G. Mauricio 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) 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 Problems1. Consider an array grade[8], w=2, base =100. Look for the address of grade[5] and NE Given: NE = UB – LB + 1 UB=7 =7–0+1 LB = 0 =8 w=2 base = 100 Memory Mapping Find: Loc[5] & NE Element Address 0 100 - base Solution: 1 102 Loc[k] = base + w(k-LB) 2 104 Loc[5] = 100 + 2(5-0) 3 106 = 100 + 2(5) 4 108 = 110 5 110 6 112 7 1142. 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] & NELecture 3: Types of Data Structures 4
  5. 5. CS 211 Data Structures and Algorithms by Jayson G. Mauricio Solution: Element Address Loc[k] = base + w(k-LB) -2 200 -base Loc[-1]= 200 + 3(-1 – (-2)) -1 203 = 200 + 3(1) 0 206 = 203 1 209 2 212 Loc[k] = base + w(k-LB) 3 215 Loc[3]= 200 + 3(3 – (-2)) 4 218 = 200 + 3(5) 5 220 = 2153. 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. Given: k= Loc[k] – base + LB base = 872 w UB= 3 k= 892 – 872 + (-5) NE= 9 4 w=4 =0 Find: k Memory Mapping Element Address Solution: -5 872 - base Loc[k] = base + w(k-LB) -4 876 k= Loc[k] – base + LB -3 880 w -2 884 -1 888 NE = UB – LB + 1 0 892 LB = UB – NE + 1 1 896 =3–9+1 2 900 =4–9 3 904 = -54. 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[k] = base + w(k-LB) Loc[-4] = 558 Loc[0] = 548 + 5(0 – (-6)) w=5 = 548 + 30 NE = 7 = 578 LB = -6 1st elementLecture 3: Types of Data Structures 5
  6. 6. CS 211 Data Structures and Algorithms by Jayson G. Mauricio Find: base & Loc[0] Memory Mapping Element Address Solution: -6 548 - base Loc[k] = base + w(k-LB) -5 553 Base = Loc[k] – w(k-LB) -4 558 558 = base + 5(-4 – (-6)) -3 563 558 = base + 10 -2 568 base = 558 – 10 -1 573 base = 548 0 5785. 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 Solution: Memory Mapping NE = UB – LB + 1 Element Address LB = UB – NE + 1 -4 997 = 7 – 12 + 1 -3 1004 = 8 – 12 -2 1011 = -4 -1 1018 0 1025 Loc[k] = base + w(k-Lb) 1 1032 1074 = 997 + w(7- (-4)) 2 1039 1074 – 997 = w(11) 3 1046 1074 – 997 = w(11) 4 1053 11 11 5 1060 w = 1074 – 997 6 1067 11 7 1074 w=7TWO-DIMENSIONAL ARRAY- 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 arraySyntax: <base_type> <array_name>[<index>] [<index>];e.g.Lecture 3: Types of Data Structures 6
  7. 7. CS 211 Data Structures and Algorithms by Jayson G. Mauricio int x[3][4];Illustration: 3 x 4 array in column major 3 x 4 array in row major - row varies faster than column - column varies faster than row 1 2 3 4 1 2 3 4 1 1,1 1,2 1,3 1,4 1 1,1 2,1 3,1 4,1 2 2,1 2,2 2,3 2,4 2 1,2 2,2 3,2 4,2 3 3,1 3,2 3,3 3,4 3 1,3 3,2 3,3 4,3To determine the Total number of elements: NE = M * N Where: NE = number of elements M = UB1 – LB1 + 1 N = UB2 – LB2 + 1To 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: j,k – index of an element in the arraySample problems1. Given an array with (2..5, -3..6) of integer. Base=100, w=4. Find the Loc[4,- 2] using row major. Given: Loc[j,k] = base +w[N(j-LB1) + (k-LB2)] UB1 = 5 Loc[4,-2] = 100 + 4[10(4-2) + (-2 – (-3))] LB1=2 = 100 + 4[10(4) + 1] UB2 = 6 = 100 + 4(21) LB2 = -3 = 100 + 84 base = 100 = 184 w=4 Find: Loc[4,-2] Solution: N = UB2 – LB2 + 1 = 6- (-3) + 1 = 102. 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.Lecture 3: Types of Data Structures 7
  8. 8. CS 211 Data Structures and Algorithms by Jayson G. Mauricio Given: Loc[j,k] = base + w[ M(k - LB2) + (j - LB1) ] Base = 200 Loc[10,2] = 200 + 3[25(2-1) + (10-1)] w= 3 = 200 + 3(25+9) M = 25 = 200 + 3(34) N=4 = 200 +102 = 302 Find: Loc[10,2] Solution: NE = M * N = 25 * 4 = 200Lecture 3: Types of Data Structures 8

×