Computer Science 112:

Introduction to Computer Science II

Gregory M. Kapfhammer


flickr photo shared by Billboard Art Project under a Creative Commons ( BY-NC-ND ) license

Color Scheme

Key Concept

Corresponding Diagram

In-Class Discussion

In-Class Activity

Details in the Textbook

Lists versus stacks and queues

Limitations of the stack?

Limitations of the queue?

Let's remove those limitations!

What are the trade-offs?

What are the inputs?

What are the outputs?

Organize the class into four groups

When have methods been slow?

Some example methods

Assume that the implementation uses an array

Consider the inputs and outputs of each method

Determine if the implementation is "efficient"

Justify your response concerning efficiency

get(i)

set(i,e)

add(i,e)

remove(i)

Shifting "to the right" for insertion

Shifting "to the left" for removal

O(n-i+1)

Why is this time complexity correct?

Any questions about the array-based approach?

Table 7.1 for the big-Oh time complexities

Dynamic Arrays

A fixed size is a serious limitation

Problem: too large of an array

Problem: too small of an array

Solution: operating like a hermit crab

What is the anticipated trade-off?


flickr photo shared by Kristian Golding under a Creative Commons ( BY-NC-ND ) license

Cannot actually "grow" an array

Move the contents of an array to a larger one

When the array "overflows" ...

Refer to Figure 7.3 for more details

Step 1: Create B

Step 2: Store A's elements in B

Step 3: Reassign reference to B

Later: Garbage collection of old A

What is the worst-case time complexity?

Well, maybe not so bad ...

What is the cause for optimism?

Amortization

See Section 7.2 for more details

StringBuilder stores characters in dynamic array

No wonder it is faster than standard method!

Data structure uses O(n) memory

What about shrinking the array?

Be careful not to oscillate!

Positional Lists

Iterators

Any questions about lists and iterators?