Chapter 11 Data Structures
OBJECTIVES After reading this chapter, the reader should be able to: Understand arrays and their usefulness. Understand records and the difference between an array and a record. Understand the concept of a linked list and the difference between an array and a linked list. Understand when to use an array and when to use a linked-list.
Data Structure Uses a collection of related variables that can be accessed individually or as whole. Represents a set of data items with specific relationship between them. Three types data structures : Arrays Records Linked lists
11.1 ARRAYS
array Is a fixed-size, sequenced collection of elements of the same data type. It is sequenced collection, so we identify the elements as first element, second element .. N element.
Twenty individual variables Figure 11-1 Twenty individual variables
Processing individual variables Figure 11-2 Processing individual variables
Arrays with subscripts and indexes Figure 11-3 Arrays with subscripts and indexes
Figure 11-4 Processing an array
Array Applications Frequency array Graphical representation
Frequency array Show the number of elements with the same value found in a series of numbers
Figure 11-5 Frequency array
Histogram Pictorial representation of a frequency array Printing the value of the elements to show the frequency of number in bar chart. Its called one-dimensional arrays because the data are organized linearly Two-dimensional array represent rows and column
Figure 11-6 Histogram
Two-dimensional array Figure 11-7- Part I Two-dimensional array
Figure 11-8 Memory layout
11.2 RECORDS
Records A collection of related elements, possibly of different types, having a single name Each element in a record is called a field A field : smallest elements of named data that has meaning Difference between array and records : all elements in array must be of the same type, elements in record can be the same or different types.
Figure 11-9 Records
Note: The elements in a record can be of the same or different types. But all elements in the record must be related.
11.3 LINKED LISTS
Linked list An ordered collection of data in which each element contains the location of the next elements Each elements contains two parts Data : holds the useful information, the data to be processed Link : to chain the data together
Figure 11-10 Linked lists
Figure 11-11 Node
Figure 11-12 Inserting a node
Figure 11-13 Deleting a node
Figure 11-14 Traversing a list