©brooks/cole, 2003 chapter 11 data structures. ©brooks/cole, 2003 data structure data structure...

24
©Brooks/Cole, 2003 Chapter 11 Data Structures

Post on 21-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

©Brooks/Cole, 2003

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

ARRAYSARRAYSARRAYSARRAYS11.111.1

©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

RECORDSRECORDSRECORDSRECORDS11.211.2

©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

The elements in a record can be The elements in a record can be of theof the

same or different types. But all same or different types. But all elementselements

in the record must be related. in the record must be related.

Note:Note:

©Brooks/Cole, 2003

Accessing Individual Fields

• Read book P.220

©Brooks/Cole, 2003

LINKEDLINKEDLISTSLISTS

LINKEDLINKEDLISTSLISTS

11.311.3

©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

Linked lists

Null pointerPointer variable

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