Computer Science 101

Data Abstraction

Gregory M. Kapfhammer


flickr photo by "Cowboy" Ben Alman shared under a Creative Commons (BY-NC-ND) license

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 it?

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

Connections to the previous material?

Any questions about linked lists?