Chapter 11 Data Structures BrooksCole 2003 Data Structure
Chapter 11 Data Structures ©Brooks/Cole, 2003
Data Structure • Data structure uses collection of related variables that can be accessed individually or as a whole. • There are three types of data structures; 1. Array 2. Records 3. Linked List ©Brooks/Cole, 2003
11. 1 ARRAYS ©Brooks/Cole, 2003
Figure 11 -1 Twenty individual variables ©Brooks/Cole, 2003
Figure 11 -2 Processing individual variables Read 20 times Process 20 times Print 20 times Not efficient, you need a powerful data structure such as an array ©Brooks/Cole, 2003
Figure 11 -3 Arrays with subscripts and indexes ©Brooks/Cole, 2003
Figure 11 -4 Processing an array Loop Construct makes array processing easy Indexing is the method used to refer to the variable in the array. ©Brooks/Cole, 2003
Figure 11 -5 Array Applications - Frequency Arrays ©Brooks/Cole, 2003
Figure 11 -6 Histogram ©Brooks/Cole, 2003
Figure 11 -7 - Part I Two-dimensional array ©Brooks/Cole, 2003
Figure 11 -8 Memory layout Row-major? ©Brooks/Cole, 2003
Row-Major (methods for storing multidimensional arrays in linear memory) ©Brooks/Cole, 2003
11. 2 RECORDS ©Brooks/Cole, 2003
Records • A record is a collection of related elements having a single name. • Each element is called a field. • A field is the smallest element that has meaning. • The difference between an array and a record is that all elements in an array can be of the same type however elements in records can be of different type. • Data in a record should be related to one object. ©Brooks/Cole, 2003
Figure 11 -9 Records ©Brooks/Cole, 2003
Note: The elements in a record can be of the same or different types. But all elements in the record must be related. ©Brooks/Cole, 2003
Accessing Individual Fields • Read book P. 220 ©Brooks/Cole, 2003
11. 3 LINKED LISTS ©Brooks/Cole, 2003
Inked List • A linked list is an ordered collection of data in which each element contains the location of the next element. • Each element contains two parts: data and link. • The data part holds the useful information. • The link part is used to chain data. It contains the pointer (address) that identify the next element. • Pointer variable also identifies the first element in the list. • Singly linked list contains one link to a single successor ©Brooks/Cole, 2003
Figure 11 -10 Pointer variable Linked lists Null pointer ©Brooks/Cole, 2003
Figure 11 -11 Node ©Brooks/Cole, 2003
Figure 11 -12 1. Inserting a node 1. Allocate memory for new node 2. New node points to its successor 3. Predecessor node points to new node ©Brooks/Cole, 2003
Figure 11 -13 2. Deleting a node Allocate the node to be deleted Make predecessor points to the node successor ©Brooks/Cole, 2003
Figure 11 -14 Traversing a list- changing the value, printing Walking pointer to move from a node to a node ©Brooks/Cole, 2003
- Slides: 24