Title: Chapter 21 Implementing lists : Linked implementations
1Chapter 21 Implementing lists Linked
implementations
2Linked implementations
- Structure built from collection of nodes that
reference each other. - A node
- contains reference to a list element,
- one or more variables referencing other structure
nodes.
3First linked implementation
- LinkedList Each node references node containing
next list element.
4Class Node
- Class Node, local to LinkedList, models nodes.
- Node structure
- must know an element of the List,
- must know the next Node in sequence.
- As local class of LinkedList has no methods.
5Class Node implementation
public class LinkedListltElementgt extends
AbstractListltElementgt private class Node
private Element element private Node
next // Create a Node containing specified
element. public Node (Element element)
this.element element this.next null
// end of class Node // end of class
LinkedList
6LinkedList implementation
public class LinkedListltElementgt extends
AbstractListltElementgt private int
size private Node first // Create an empty
LinkedListltElementgt. public LinkedList ()
size 0 first null
7An empty linked list
8Linked list with three entries
9Method to get the i-th node
/ The i-th node of this LinkedList. The
LinkedList must be non-empty. require 0 lt i
i lt this.size() / private Node getNode (int
i) Node p first int pos 0 // p is
pos-th Node while (pos ! i) p
p.next pos pos 1 return p
10Method to get the i-th node i 2
LinkedList
size
4
first
p
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
11Method to get the i-th node i 2
LinkedList
size
4
first
p
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
12Method to get the i-th node i 2
LinkedList
size
4
first
p
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
13Implementing LinkedList methods
public Element get (int index) Node p
getNode(index) return p.element
- Note method get
- uses getNode
- get(i) will take i1 steps to get the element.
14Method remove(int)
- Given an index referencing a node to be removed
- Find previous node referencing it.
- Update that nodes next.
15Method remove(int)
LinkedList
Element to be removed
size
4
first
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
16Method remove(int)
LinkedList
Element to be removed
size
4
first
p
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
17Method remove(int)
18Method remove(int)
19Method remove(int)
LinkedList
Element to be removed
size
4
first
p
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
20Method remove(int)
LinkedList
Element to be removed
size
3
first
p
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
21Method remove(int), special case
- Recall getNode requires argument gt 0.
- If index is 0, its not legal to write
- Node p getNode(index-1)
- Special case index 0 removes first linked
element. - instance variable first of LinkedList must be
modified.
22Method remove(int)
LinkedList
size
4
node to be removed
first
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
23Method remove(int)
LinkedList
size
4
node to be removed
first
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
copy
24Method remove(int)
node to be removed
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
25Method remove(int)
node to be removed
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
26Implementation of remove(int)
public void remove (int index) if (index
0) first first.next else Node p
getNode(index-1) p.next p.next.next size
size - 1
27Method add (int, Element)
- To add an element at the i-th position
- must find the element with index i-1
- special case to consider when i is 0.
28Method add(int, Element)
- Add an element at index 2.
- Must find previous node.
29Method add(int, Element)
30Method add(int, Element)
31Method add(int, Element)
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
32Method add(int, Element)
- LinkedList size is incremented.
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
33Method add(int, Element)
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
34Method add(int, Element)
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
35Method add(int, Element)
Node
Node
Node
Node
next
next
next
next
element
element
element
element
É
É
É
É
36Method add (int, Element)
- public void add (int index, Element element)
- Node newNode new Node(element)
- if (index 0)
- newNode.next first
- first newNode
- else
- Node p getNode(index-1)
- newNode.next p.next
- p.next newNode
-
- size size 1
37Linked list variations
- Adding a reference to last node makes adding to
end of list constant.
38LinkedList adding a last reference
- Creates special cases in other methods. For
instance, remove
public void remove (int index) if (size 1)
// remove only element first null last
null else if (index 0) //remove first
element first first.next else Node p
getNode(index-1) p.next p.next.next
if (index size-1) //last element removed
last p size size - 1
39Header nodes
- dummy node
- Always present at front of list,
- contains no element,
- first always references header node,
- first is never null.
- Reduces special cases in the implementation of
some methods.
40Header nodes
41Circular lists
- Last node references the first.
42Doubly-linked lists
43Doubly-linked list constructor
- Assuming Header extends Node DoublyLinkedList
constructor creates header Node, and links it to
itself.
public DoublyLinkedList () size 0 header
new Header() header.next header header.previ
ous header
44Doubly-linked list constructor
45Doubly-linked lists add(Element)
Header
A
B
C
New
46Doubly-linked lists add(Element)
- new node previous is node C
Header
A
B
C
New
47Doubly-linked lists add(Element)
Header
A
B
C
New
48Doubly-linked lists add(Element)
- Header previous is new node
Header
A
B
C
New
49Doubly-linked lists add(Element)
Header
A
B
C
New
50Doubly-linked lists remove(1)
51Doubly-linked lists remove(1)
Header
A
B
C
52Doubly-linked lists remove(1)
Header
A
B
C
53Linked Lists limitations
- Element access by index is linear.
- Would not use a linked implementation if primary
operation is to access randomly by index. - Method get uses getNode requires i steps to get
Node with index i.
54Linked Lists limitations indexOf(Element)
- indexOf uses method get.
- If element not found, get executes n times, with
index values 0 through n-1 - value of i 0 1 2 n-1
- steps of get(i) 0 1 2 n-1
- Steps require 0 1 2 (n-1) (n2-n)/2.
- Method is quadratic.
55Re-implementation of indexOf
public int indexOf (Element element) Node p
first int pos 0 while (p ! null
!element.equals(p.element)) p p.next pos
pos1 if (p null) return
-1 else return pos
- Method is linear.
- But uses private members of the class LinkedList.
56Dynamic memory allocation
- Recall memory space for method variables
parameters and local variables is allocated
when the method is invoked, and reclaimed
(deallocated) when the method completes. - This is called automatic allocation and
deallocation. - Space for an objects instance variables is
allocated when the object is created. This is
sometimes called dynamic allocation.
57Dynamic memory allocation
- In an array-based list implementation, memory
space for the array elements is allocated when
the array is created. -
- In a linked implementation, space required for a
node is allocated when the node is created, i.e.
when an item is added to the list. - But when is this space deallocated?
58Dynamic memory allocation
- The Java run-time system implements a facility
called garbage collection. - Dynamically allocated space that can no longer be
accessed is termed garbage. - If we create an object and then lose all
references to the object, the memory space
occupied by the object becomes garbage. - The run-time system continuously looks for
garbage and reclaims the space collects the
garbage.
59Dynamic memory allocation
- Many programming languages do not include garbage
collection as part of their run-time systems. - These languages require programs to explicitly
deallocate dynamically allocated space. - Problem difficult to know when an object is no
longer accessible and can be safely deallocated. - Subtle errors
- Memory leaks garbage not being recognized and
reclaimed - Dangling reference space still accessible being
mistakenly reclaimed as garbage.