©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their...

Post on 18-Dec-2015

227 views 7 download

Tags:

Transcript of ©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their...

©Brooks/Cole, 2003

Chapter 11

Data Structures

©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.

©Brooks/Cole, 2003

ARRAYSARRAYSARRAYSARRAYS11.111.1

©Brooks/Cole, 2003

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 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)

©Brooks/Cole, 2003

Figure 11-5

Frequency array

©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” storageRow-major” storage

©Brooks/Cole, 2003

RECORDSRECORDSRECORDSRECORDS11.211.2

©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

©Brooks/Cole, 2003

Figure 11-9

Records

©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:

©Brooks/Cole, 2003

LINKEDLINKEDLISTSLISTS

LINKEDLINKEDLISTSLISTS

11.311.3

©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)

©Brooks/Cole, 2003

Figure 11-10

Linked lists

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

©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

©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

©Brooks/Cole, 2003

Figure 11-12

Inserting a node

©Brooks/Cole, 2003

Figure 11-13

Deleting a node

©Brooks/Cole, 2003

Figure 11-14

Traversing a list

©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