Transcript
Page 1: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

1

Chapter 2. Linked Lists

- Singly linked lists- Doubly linked lists

aka lecture 3

Page 2: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

2

Dynamic data structures

• Data collections (e.g. stored library records) can vary considerably in size. Arrays not always best solution:– Inserting/deleting a new element requires much of array

to be rewritten– Array size is fixed, must be estimated before use– If only few items held, much of array (hence memory) is

wasted

• Solution: dynamic data structures (linked data structures)

- don’t need to know how many items to expect

- can increase/decrease memory when items added/deleted

Page 3: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

3

Examples

• Linked lists• Trees

– Binary trees– Binary search trees– AVL trees– B trees

All use objects which are created dynamically, using memory from a special storage area (pool/heap).

Will look at all of these

Page 4: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

4

Singly Linked Lists (Goodrich § 3.2)

• A singly linked list is a concrete data structure consisting of a sequence of nodes

• Each node stores– element– link to the next node (or null)

• First node in list referred to as head

next

elem node

A B C D

head

Page 5: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

next

node

element

For an easy introduction we will have a list of Strings

The thing above is our Node

Page 6: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

The Node Class for String objects

next

node

element

Page 7: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

The Node Class for String objects

Sometimes called “head”

next

node

element

Page 8: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

The Node Class for String objects

Sometimes called “tail”

next

node

element

Page 9: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

The Node Class for String objects

next

node

element

constructors

Page 10: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

The Node Class for String objects

next

node

element

get

Page 11: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

The Node Class for String objects

next

node

element

set

Page 12: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Over loading

next

node

element

Page 13: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Don’t like

Page 14: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

better

Page 15: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

The list

Page 16: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3
Page 17: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

head

size

Page 18: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

head = null

size = 0

Page 19: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3
Page 20: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3
Page 21: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3
Page 22: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

if head == null return true; else return false;

NO!

Page 23: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3
Page 24: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

head

size = 3

dog owl catStringList L =

L.addFront(“pig”)

head

size = 4StringList L =

dog owl catpig

Page 25: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Inserting at the Head

Allocate a new node

Page 26: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Inserting at the Head

Allocate a new node

Page 27: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Inserting at the Head

Allocate a new node

New node points to old head

Page 28: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Inserting at the Head

Allocate a new node

New node points to old head

Head points to new node

Page 29: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Inserting at the Head

Allocate a new node

New node points to old head

Head points to new node

Increase size counter

Page 30: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Removing at the Head

1. Update head to point to next node in the list

Page 31: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Removing at the Head

1. Update head to point to next node in the list

2. Allow garbage collector to reclaim the former first node

Page 32: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

head

size = 4StringList L =

dog owl catpig

head

size = 3StringList L =

dog owl catpig

YIKES!

Page 33: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Removing at the Tail

• Removing at the tail of a singly linked list is not efficient!

• There is no constant-time way to update the tail to point to the previous node

-Would need to keep a record of which node is the last node and which node points to the last node (even then can’t do it in constant time!)

Page 34: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Java code for a singly linked list of Strings

• In our implementation instance variables are head (reference to head node) and size (number of nodes currently in list)

- Different implementations have different instance variables (some store the head and last/tail, some size (some not)). Will see later, depends what we actually want to do with our list.

• Our implementation is specific to strings. You will need to adapt it to accept values of other types - we will see a generic linked list later.

Page 35: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

We have seen this

Page 36: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Simple Remove and Exception

We have seen this

But not this

Page 37: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

The toString method

Iterating over a list

Page 38: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

The toString method

Iterating over a list

This is a pointer that traverses the list

Page 39: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

The toString method

Iterating over a list

Page 40: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

The toString method

Iterating over a list

What’s happening here?

Page 41: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

The toString method

Iterating over a list

What’s happening here?

implicit cursor.toString()

Page 42: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

The toString method

Iterating over a list

Page 43: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

The toString method

Iterating over a list

So we don’t have a list (pig,dog,owl,cat,)

Page 44: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Iterating over a list

Iterating over a list

… and this is a very nice template (pattern) for iterating over a linked list

Page 45: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

45

Checking to see if a node containing a given value is in the list:Use a variable temp of type Node, initially pointing to (node pointed to by) head and progressively pointing to nodes along list until temp points to node containing value of interest, or temp points to null (i.e. end of list):

> Searching for “when”

It’s easy when you know howhead

temp temp tempreturn true

It’s easy when you know howhead

> Searching for “bucket”temp temp temp temp temp temp temp

return falseADS2 Lecture 4

Linear Search: isPresent

(Alice’s slide)

Page 46: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Linear Search: isPresent

Page 47: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Linear Search: isPresent

Assume we haven’t foundwhat we are looking for

Page 48: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Linear Search: isPresent

Our travelling cursor

Page 49: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Linear Search: isPresent

Quit if we have hit end-of-list or we found what we are looking for

Page 50: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Linear Search: isPresent

Quit if we have hit end-of-list or we found what we are looking for

NOTE: we quit if “X or Y” is same as we continue if “¬X and ¬Y” De Morgan’s law!!!!

Page 51: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Linear Search: isPresent

Is this what we are looking for?

Page 52: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Linear Search: isPresent

Move the cursor down the list

Page 53: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Linear Search: isPresent

Did we find it?

Page 54: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Linear Search: isPresent

• note naming conventions (toString, isPresent, …)• note that we do NOT do things like

• if (x > y) then b = true else b = false• please …• note, no assumptions about order in data• how could we use order if we had it?

• note, I use “cursor” rather than “temp” (why?)

Page 55: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Linear Search: isPresent

NOTE similarity

Page 56: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Linear Search: isPresent

Is this better?

Page 57: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Linear Search: isPresent

Is this better?

Page 58: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Linear Search: isPresent

Is this better?

Page 59: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Linear Search: isPresent

Is this better?

Page 60: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

Linear Search: isPresent

Is this better?

Page 61: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

61ADS2 Lecture 3

Page 62: 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

62

Inserting at the Tail

1. Allocate a new node2. Insert new element3. Have new node point to

null4. Have old tail node point

to new node5. Update tail to point to

new node

Exercise for you:•Start off with an empty list and add the following Strings, in the order given. This time insert at the tail each time. •Starting from the head read off the Strings.

“how”, “know”, “you”, “when”, “easy”, “it’s”

Note, need to keep a record of the “last” (or “tail”) node. In Java do this via extra instance variable in list. See lecture 5.

ADS2 Lecture 3


Top Related