chapter 9: data structures i presentation slides for java software solutions for ap* computer...

25
Chapter 9: Data Structures Chapter 9: Data Structures I I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java Software Solutions is published by Addison-Wesley Presentation slides are copyright 2002 by John Lewis, William Loftus, and Cara Cocking. All rights reserved. Instructors using the textbook may use and modify these slides for pedagogical purposes.

Upload: darren-yarboro

Post on 01-Apr-2015

258 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

Chapter 9: Data Structures I Chapter 9: Data Structures I

Presentation slides for

Java Software Solutionsfor AP* Computer Science

by John Lewis, William Loftus, and Cara Cocking

Java Software Solutions is published by Addison-Wesley

Presentation slides are copyright 2002 by John Lewis, William Loftus, and Cara Cocking. All rights reserved.

Instructors using the textbook may use and modify these slides for pedagogical purposes.

Page 2: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

2

Data StructuresData Structures

Now we can now explore some convenient techniques for organizing and managing information

Chapter 9 focuses on:• collections• Abstract Data Types (ADTs)• dynamic structures and linked lists• queues and stacks

Page 3: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

3

CollectionsCollections

A collection is an object that serves as a repository for other objects

A collection usually provides services such as adding, removing, and otherwise managing the elements it contains

Sometimes the elements in a collection are ordered, sometimes they are not

Sometimes collections are homogeneous, sometimes the are heterogeneous

Page 4: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

4

Abstract Data TypesAbstract Data Types

Collections can be implemented in many different ways

An abstract data type (ADT) is an organized collection of information and a set of operations used to manage that information

The set of operations defines the interface to the ADT

As long as the ADT fulfills the promises of the interface, it doesn't really matter how the ADT is implemented

Objects are a perfect programming mechanism to create ADTs because their internal details are encapsulated

Page 5: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

5

AbstractionAbstraction

Our data structures should be abstractions

That is, they should hide unneeded details

We want to separate the interface of the structure from its underlying implementation

This helps manage complexity and makes it possible to change the implementation without changing the interface

Page 6: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

6

Collection ClassesCollection Classes

The Java standard library contains several classes that represent collections, often referred to as the Java Collections API

Their underlying implementation is implied in the class names such as ArrayList and LinkedList

Several interfaces are used to define operations on the collections, such as List, Set, and Map

Page 7: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

7

Static vs. Dynamic StructuresStatic vs. Dynamic Structures

A static data structure has a fixed size

This meaning is different from the meaning of the static modifier

Arrays are static; once you define the number of elements it can hold, the number doesn’t change

A dynamic data structure grows and shrinks at execution time as required by its contents

A dynamic data structure is implemented using links

Page 8: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

8

Object ReferencesObject References

Recall that an object reference is a variable that stores the address of an object

A reference also can be called a pointer

References often are depicted graphically:

studentJohn Smith

407253.58

Page 9: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

9

References as LinksReferences as Links

Object references can be used to create links between objects

Suppose a Student class contains a reference to another Student object

John Smith407253.57

Jane Jones588213.72

Page 10: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

10

References as LinksReferences as Links

References can be used to create a variety of linked structures, such as a linked list:

studentList

Page 11: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

11

Intermediate NodesIntermediate Nodes

The objects being stored should not be concerned with the details of the data structure in which they may be stored

For example, the Student class should not have to store a link to the next Student object in the list

Instead, we can use a separate node class with two parts: 1) a reference to an independent object and 2) a link to the next node in the list

The internal representation becomes a linked list of nodes

See ListNode.java (pg 516)

Page 12: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

12

Magazine CollectionMagazine Collection

Let’s explore an example of a collection of Magazine objects

The collection is managed by the MagazineList class, which contains a linked list of ListNode objects

The data portion of each ListNode is a Magazine object

See ListNode.java (page 516) See MagazineRack.java (page 521) See MagazineList.java (page 522) See Magazine.java (page 523)

Page 13: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

13

Inserting a NodeInserting a Node

A method called insert could be defined to add a node anywhere in the list, to keep it sorted, for example

See Figures 9.2 and 9.3

Page 14: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

14

Deleting a NodeDeleting a Node

A method called delete could be defined to remove a node from the list

See Figures 9.4 and 9.5

Page 15: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

15

Other Dynamic List RepresentationsOther Dynamic List Representations

It may be convenient to implement as list as a doubly linked list, with next and previous references

list

Page 16: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

16

Other Dynamic List ImplementationsOther Dynamic List Implementations

It may be convenient to use a separate header node, with a count and references to both the front and rear of the list

count: 4frontrear

list

Page 17: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

17

Other Dynamic List ImplementationsOther Dynamic List Implementations

A linked list can be circularly linked in which case the last node in the list points to the first node in the list

If the linked list is doubly linked, the first node in the list also points to the last node in the list

The representation should facilitate the intended operations and should make them easy to implement

Page 18: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

18

QueuesQueues

A queue is similar to a list but adds items only to the rear of the list and removes them only from the front

It is called a FIFO data structure: First-In, First-Out

Analogy: a line of people at a bank teller’s window

enqueue dequeue

Page 19: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

19

QueuesQueues

We can define the operations for a queue• enqueue - add an item to the rear of the queue• dequeue (or serve) - remove an item from the front of the

queue• isEmpty - returns true if the queue is empty

As with our linked list example, by storing generic Object references, any object can be stored in the queue

Queues often are helpful in simulations or any situation in which items get “backed up” while awaiting processing

Page 20: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

20

Priority QueuesPriority Queues

In a priority queue, some elements get to “cut in line”

The enqueue and isEmpty operations behave the same as with normal queues

The dequeue operation removes the element with the highest priority

Page 21: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

21

StacksStacks

A stack ADT is also linear, like a list or a queue

Items are added and removed from only one end of a stack

It is therefore LIFO: Last-In, First-Out

Analogies: a stack of plates in a cupboard, a stack of bills to be paid, or a stack of hay bales in a barn

Page 22: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

22

StacksStacks

Stacks often are drawn vertically:

poppush

Page 23: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

23

StacksStacks

Some stack operations:• push - add an item to the top of the stack• pop - remove an item from the top of the stack• peekTop - retrieves the top item without removing it• isEmpty - returns true if the stack is empty

A stack can be represented by a singly-linked list; it doesn’t matter whether the references point from the top toward the bottom or vice versa

A stack can be represented by an array, but the new item should be placed in the next available place in the array rather than at the end of the array

Page 24: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

24

StacksStacks

Like ArrayList operations, the Stack operations operate on Object references

See Decode.java (page 531) See ArrayStack.java (page 532)

Page 25: Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java

25

SummarySummary

Chapter 9 has focused on:• collections• Abstract Data Types (ADTs)• dynamic structures and linked lists• queues and stacks