25
©Brooks/Cole, 2003 Chapter 11 Data Structures

©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

Embed Size (px)

Citation preview

Page 1: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Chapter 11

Data Structures

Page 2: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Understand arrays and their usefulness.Understand arrays and their usefulness.

Understand records and the difference between an array and Understand records and the difference between an array and a record.a record.

Understand the concept of a linked list and the differenceUnderstand the concept of a linked list and the differencebetween an array and a linked list.between an array and a linked list.

After reading this chapter, the reader should After reading this chapter, the reader should be able to:be able to:

OOBJECTIVESBJECTIVES

Understand when to use an array and when to use a linked-list.Understand when to use an array and when to use a linked-list.

Page 3: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

ARRAYSARRAYSARRAYSARRAYS11.111.1

Page 4: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Figure 11-1

Twenty individual variables

Page 5: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Figure 11-2

Processing individual variables

Page 6: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Figure 11-3

Arrays with subscripts(下標 ) and indexes

Page 7: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Figure 11-4

Processing an array

Page 8: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Array ApplicationsArray Applications

Frequency arraysFrequency arrays– A A frequency arrayfrequency array shows the shows the number number of of

elements with the elements with the same valuesame value found in a found in a series of numbers. (Fig. 11.5)series of numbers. (Fig. 11.5)

HistogramsHistograms– A A histogramhistogram is a pictorial ( is a pictorial ( 圖示的圖示的 ) )

representation of a frequency array. (Fig. representation of a frequency array. (Fig. 11.6)11.6)

Page 9: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Figure 11-5

Frequency array

Page 10: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Figure 11-6

Histogram

Page 11: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Figure 11-7- Part I

Two-dimensional array

Page 12: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Figure 11-8

Memory layout

““Row-major” storageRow-major” storage

Page 13: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

RECORDSRECORDSRECORDSRECORDS11.211.2

Page 14: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

RecordsRecords

A A record record is a collection of related is a collection of related elements, possibly of different types, elements, possibly of different types, having a single name.having a single name.

Each element in a record is called a Each element in a record is called a field.field.

A A fieldfield is the smallest element of is the smallest element of named data that has meaning.named data that has meaning.

Fig. 11.9Fig. 11.9

Page 15: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Figure 11-9

Records

Page 16: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

The elements in a record can The elements in a record can be of the same or different be of the same or different

types. But all elements in the types. But all elements in the record must be related. record must be related.

Note:Note:

Page 17: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

LINKEDLINKEDLISTSLISTS

LINKEDLINKEDLISTSLISTS

11.311.3

Page 18: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Linked listsLinked lists

A A linked listlinked list is a ordered collection of is a ordered collection of data in which each element contains data in which each element contains the location of the next element.the location of the next element.

Each element contains two parts:Each element contains two parts:– Data:Data: the data parts holds the useful the data parts holds the useful

informationinformation– Link:Link: the link is use to chain the data the link is use to chain the data

togethertogether Example: singly linked list (Fig. 11.10)Example: singly linked list (Fig. 11.10)

Page 19: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Figure 11-10

Linked lists

Null pointer: indicate the end of the Null pointer: indicate the end of the listlist

Page 20: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Figure 11-11

Node

A node in a linked list is a record that A node in a linked list is a record that has at least two fields: one contains has at least two fields: one contains the data, and the other contains the the data, and the other contains the address of the next node in the address of the next node in the sequencesequence

Page 21: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Operations on linked listsOperations on linked lists

Inserting a nodeInserting a node Deleting a nodeDeleting a node Searching a listSearching a list Retrieving a nodeRetrieving a node Traversing a listTraversing a list Coping a list … and so onCoping a list … and so on

Page 22: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Figure 11-12

Inserting a node

Page 23: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Figure 11-13

Deleting a node

Page 24: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Figure 11-14

Traversing a list

Page 25: ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between

©Brooks/Cole, 2003

Key termsKey terms ArrayArray Data structureData structure FieldField Frequency arrayFrequency array HistogramHistogram LinkLink Linked listLinked list LoopLoop MemoryMemory Node Node Null pointerNull pointer

One-dimensional arrayOne-dimensional array PointerPointer RecordRecord Row-major storageRow-major storage Search a listSearch a list Singly linked listSingly linked list SubscriptSubscript Two-dimensional arrayTwo-dimensional array Variable Variable