Implementations of The List ADT Properties of Array and Linked Implementations Separate and Inner Node Classes Singly and Doubly Linked Lists
Implementations of The List ADT Properties of Array and Linked Implementations Separate and Inner Node Classes Singly and Doubly Linked Lists
05
Introduction
Any interface specification can be correctly
implemented in many ways
However, different correct implementations may have
different performance characteristics
It is important to know these characteristics, as they
affect the performance of the resulting software
Collection ADT's
Array Characteristics
An array is a homogeneous data structure: all elements are
of the same type
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
SLNode<E>
/**
*
The structure of a node in a singly linked list
*/
public class SLNode<E> {
private E
data;
// the data field
private SLNode<E> next;
// link to successor
The self-referential
(recursive) part of
the definition
/**
* Create an empty <tt>SLNode</tt> object.
*/
public SLNode() {
this.data = null;
this.next = null;
}
/**
* Create an <tt>SLNode</tt> that stores <tt>theElement</tt> and
* whose successor is <tt>theSuccessor</tt>.
* @param theElement
the element to store in this node
* @param theSuccessor this nodes successor
*/
public SLNode( E theElement, SLNode<E> theSuccessor ) {
this.data = theElement;
this.next = theSuccessor;
}
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
SLNode<E>
/**
*
Successor accessor
*/
public SLNode<E> getSuccessor() {
return this.next;
}
/**
* Successor mutator
*/
public void setSuccessor(SLNode<E> n) {
this.next = n;
}
/**
*
Element accessor
*/
public E getData() {
return this.data;
}
/**
* Element mutator
*/
public void setData(E e) {
this.data = e;
}
SLNode instance
variables are private; so
we need to define public
accessors and mutators
SLNode construction
next
data
node2
i1
next
data
next
data
15
i1
next
data
next
data
15
temp
ls
head
size
ls
next
data
head
size
next
data
next
data
next
data
4
15
10
40
77
ls
next
data
head
tail
next
data
size
ls
next
data
head
tail
next
data
15
next
data
size
next
data
next
data
10
next
data
40
77
2. Set the new nodes next field to point where head points
newnode.setSuccessor(head);
4. Increment size by 1
size++;
newnode
next
data
77
element
newnode
next
data
77
element
2. Set the new nodes next field to point where head points
newnode.setSuccessor( head );
newnode
next
data
77
element
head
size
next
data
next
data
next
data
4
15
newnode
10
40
77
next
data
77
element
head
size
next
data
next
data
4
15
newnode
next
data
10
40
77
next
data
77
element
2. Set the new nodes next field to point where head points
newnode.setSuccessor( head );
head
size
next
data
next
data
5
15
newnode
next
data
10
40
next
data
77
element
77
7. Increment size by 1
size++;
}
head
size
next
data
next
data
next
data
4
15
newnode
10
40
77
next
data
77
element
head
size
next
data
next
data
4
15
newnode
next
data
10
40
77
next
data
77
element
head
size
next
data
next
data
next
data
5
15
10
newnode
40
77
next
data
77
element
index == 0 ?
index == size ?
2. Set the new nodes next to be same as dummy head node's next
newnode.setSuccessor(head.getSuccessor());
4. Increment size by 1
size++;
E element = target.getData();
cursor.setSuccessor( target.getSuccessor() );
6. Decrement size
9
size--;
return element;
head
size
next
data
next
data
next
data
4
15
10
40
77
cursor
next
data
head
size
next
data
next
data
next
data
3
15
10
40
77
import java.util.*;
/**
*
Outer class: List<E>
*/
public class SinglyLinkedList<E> implements java.util.List<E> {
private Node<E> head;
// pointer to first element (not dummy)
private int size;
// number of nodes in this List
/**
* private static inner class: Node<T>
*/
private static class Node<T> {
private T data; // the data field
private Node<T> next; // link to successor
private Node(T data) { // inner class constructor
this.data = data;
}
}
public SinglyLinkedList () {
// outer class constructor
head = null; // null head means empty list
size = 0;
}
2. Set the new nodes next field to point where head is pointing
newnode.next = head;
4. Increment size by 1
size++;
Doubly-Linked Lists
The singly-linked list is unidirectional
At the expense of an additional link (and the
consequent code complexity) we can have a
bidirectional list
We need to add a
second link
attribute to the
node definition
Cost
(1)
(1)
(n)
resize
find by index
(1)
find by target
(n)
Cost
Singly Linked
Doubly Linked
(n)
(n)
(1)
(1)
(n)
(1)
(n)
(n)
resize
N/A
N/A
find by index
(n)
(n)
find by target
(n)
(n)
Next time
Reading: Gray, Ch 2