©Brooks/Cole, 2003 Chapter 11 Data Structures
©Brooks/Cole, 2003 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. After reading this chapter, the reader should be able to: O BJECTIVES Understand when to use an array and when to use a linked-list.
©Brooks/Cole, 2003 ARRAYSARRAYS 11.1
Figure 11-1 Twenty individual variables
©Brooks/Cole, 2003 Figure 11-2 Processing individual variables
©Brooks/Cole, 2003 Figure 11-3 Arrays with subscripts( 下標 ) and indexes
©Brooks/Cole, 2003 Figure 11-4 Processing an array
©Brooks/Cole, 2003 Array Applications Frequency arrays Frequency arrays –A frequency array shows the number of elements with the same value found in a series of numbers. (Fig. 11.5) Histograms Histograms –A histogram is a pictorial ( 圖示的 ) representation of a frequency array. (Fig. 11.6)
©Brooks/Cole, 2003 Figure 11-5 Frequency array
©Brooks/Cole, 2003 Figure 11-6 Histogram
©Brooks/Cole, 2003 Figure Part I Two-dimensional array
©Brooks/Cole, 2003 Figure 11-8 Memory layout “ Row-major ” storage “ Row-major ” storage
©Brooks/Cole, 2003 RECORDSRECORDS 11.2
Records A record is a collection of related elements, possibly of different types, having a single name. A record is a collection of related elements, possibly of different types, having a single name. Each element in a record is called a field. Each element in a record is called a field. A field is the smallest element of named data that has meaning. A field is the smallest element of named data that has meaning. Fig Fig. 11.9
©Brooks/Cole, 2003 Figure 11-9 Records
©Brooks/Cole, 2003 The elements in a record can be of the same or different types. But all elements in the record must be related. Note:
©Brooks/Cole, 2003 LINKEDLISTSLINKEDLISTS 11.3
Linked lists A linked list is a ordered collection of data in which each element contains the location of the next element. A linked list is a ordered collection of data in which each element contains the location of the next element. Each element contains two parts: Each element contains two parts: –Data: the data parts holds the useful information –Link: the link is use to chain the data together Example: singly linked list (Fig ) Example: singly linked list (Fig )
©Brooks/Cole, 2003 Figure Linked lists Null pointer: indicate the end of the list Null pointer: indicate the end of the list
©Brooks/Cole, 2003 Figure Node A node in a linked list is a record that has at least two fields: one contains the data, and the other contains the address of the next node in the sequence A node in a linked list is a record that has at least two fields: one contains the data, and the other contains the address of the next node in the sequence
©Brooks/Cole, 2003 Operations on linked lists Inserting a node Inserting a node Deleting a node Deleting a node Searching a list Searching a list Retrieving a node Retrieving a node Traversing a list Traversing a list Coping a list … and so on Coping a list … and so on
©Brooks/Cole, 2003 Figure Inserting a node
©Brooks/Cole, 2003 Figure Deleting a node
©Brooks/Cole, 2003 Figure Traversing a list
©Brooks/Cole, 2003 Key terms Array Array Data structure Data structure Field Field Frequency array Frequency array Histogram Histogram Link Link Linked list Linked list Loop Loop Memory Memory Node Node Null pointer Null pointer One-dimensional array One-dimensional array Pointer Pointer Record Record Row-major storage Row-major storage Search a list Search a list Singly linked list Singly linked list Subscript Subscript Two-dimensional array Two-dimensional array Variable Variable