Computer Science 112:

Introduction to Computer Science II

Gregory M. Kapfhammer


flickr photo by by physician1977 shared under a CC Public Domain Dedication (CC0)

Color Scheme

Key Concept

Corresponding Diagram

In-Class Discussion

In-Class Activity

Details in the Textbook

What have we studied so far?

Key Topics

Algorithms

Data structures

Software engineering tools

Java programming language

Algorithm evaluation techniques

Data structures, algorithms, and efficiency

Linked Lists

Collection of nodes

Form a linear sequence

Singly-linked list ...

Node stores data and a next pointer

Doubly-linked list ...

Node stores data and a next and previous pointer

What does this look like in memory?


flickr photo shared by TED Translators under a Creative Commons ( BY-SA ) license

Designated nodes: Head and tail of the linked list

Traversal from head to the tail

Why would we perform this operation?

How costly is this operation?

Names: "Link hopping" or "pointer hopping"

Restricted modes of interaction

Singly Linked List

addFirst

Insert an element at the head of a linked list

See Code Fragment 3.11 for the algorithm

See Figure 3.12 for an illustrative diagram

What is the performance of this algorithm? Why?

addLast

Insert an element at the tail of a linked list

See Code Fragment 3.12 for the algorithm

See Figure 3.13 for an illustrative diagram

What is the performance of this algorithm? Why?

What if we did not maintain a tail reference?

removeFirst

Remove an element at the head of a linked list

See Code Fragment 3.13 for the algorithm

See Figure 3.14 for an illustrative diagram

What is the performance of this algorithm? Why?

removeLast

Remove an element at the tail of a linked list

The textbook does not present this algorithm

But, discussing this algorithm is important!

How would this algorithm work?

What is the performance of this algorithm? Why?

The linked list has a key deficiency! How to fix?

Doubly Linked List to the rescue!

Store a reference to the next and previous node

What are the trade-offs?

Header and trailer sentinels

Benefits: always insert between two nodes

Organize yourselves into teams of two or three

Review the methods on pages 135 - 137

What is the worst-case time complexity?

Why is the removeLast method fast?

Circularly linked lists

Round-robin scheduling in operating system

Any questions about linked lists?