Chapter 2 Linked Lists Singly linked lists Doubly

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

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 2

Examples • Linked lists • Trees – Binary trees – Binary search trees – AVL trees – B trees Will look at all of these All use objects which are created dynamically, using memory from a special storage area (pool/heap). 3

Singly Linked Lists (Goodrich § 3. 2) • A singly linked list is a 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 node elem head A B C D 4

For an easy introduction we will have a list of Strings ele m next en t node The thing above is our Node

The Node Class for String objects ele me nt node next

The Node Class for String objects ele me nt node next

The Node Class for String objects ele me nt node next

The Node Class for String objects constructors ele me nt node next

The Node Class for String objects getters ele me nt node next

The Node Class for String objects setters ele me nt node next

Overriding ele me nt node next


The list


head size

head = null size = 0





String. List L = head dog owl cat size = 3 L. add. Front(“pig”) String. List L = head size = 4 pig dog owl cat

Inserting at the Head Allocate a new node

Inserting at the Head Allocate a new node

Inserting at the Head Allocate a new node New node points to old head

Inserting at the Head Allocate a new node New node points to old head Head points to new node

Inserting at the Head Allocate a new node New node points to old head Head points to new node Increase size counter

Removing at the Head 1. Update head to point to next node in the list

Removing at the Head 1. 2. Update head to point to next node in the list Allow garbage collector to reclaim the former first node

String. List L = head pig dog owl cat size = 4 head pig dog owl size = 3 YIKES! cat

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!)

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.

We have seen this

Simple Remove and Exception We have seen this But not this

The to. String method Iterating over a list

The to. String method This is a pointer that traverses the list Iterating over a list

The to. String method Iterating over a list

The to. String method What’s happening here? Iterating over a list

The to. String method What’s happening here? Iterating over a list implicit cursor. to. String()

The to. String method Iterating over a list

The to. String method So we don’t have a list (pig, dog, owl, cat, ) Iterating over a list

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

Linear Search: is. Present 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” temp head It’s temp return true temp when you know how temp temp easy when you know how easy > Searching for “bucket” temp head It’s ADS 2 Lecture 4 temp return false 43

Linear Search: is. Present

Linear Search: is. Present Assume we haven’t found what we are looking for

Linear Search: is. Present Our travelling cursor

Linear Search: is. Present Quit if we have hit end-of-list or we found what we are looking for

Linear Search: is. Present 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!!!!

Linear Search: is. Present Is this what we are looking for?

Linear Search: is. Present Move the cursor down the list

Linear Search: is. Present Did we find it?

Linear Search: is. Present • note naming conventions (to. String, is. Present, …) • 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? )

Linear Search: is. Present NOTE similarity

Linear Search: is. Present Is this better?

Linear Search: is. Present Is this better?

Linear Search: is. Present Is this better?

Linear Search: is. Present Is this better?

Linear Search: is. Present Is this better?


ADS 2 Lecture 3 60

Inserting at the Tail 1. 2. 3. 4. 5. Allocate a new node Insert new element Have new node point to null Have old tail node point to new node Update tail to point to new node 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. 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” ADS 2 Lecture 3 61
- Slides: 61