Implementing Lists
Implementing Lists
Chapter 24
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
1
Objectives
To design common features of lists in an
interface and provide skeleton implementation in
an abstract class,
To design and implement a dynamic list using an
array,
To design and implement a dynamic list using a
linked data structure.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
2
Lists
The common operations on a list are usually the
following:
– Retrieve an element from this list.
– Insert a new element to this list.
– Delete an element from this list.
– Find how many elements are in this list.
– Find if an element is in this list.
– Find if this list is empty.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
3
Two Ways to Implement Lists
There are two ways to implement a list.
Using arrays.
– An array is used as the underlying container.
– The array is dynamically created.
If the capacity of the array is exceeded, create a new larger array and
copy all the elements from the current array to the new array.
Using linked list.
– A linked data structure is used as the underlying container.
– A linked data structure consists of nodes.
– Each node is dynamically created to hold an element.
– All the nodes are linked together to form a list.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
4
Design of ArrayList and LinkedList
Let’s name these two classes: MyArrayList and MyLinkedList.
– These two classes have common operations, but different data fields.
– The common operations can be generalized in an interface or an abstract
class.
– A good strategy is to combine the virtues of interfaces and abstract classes
by providing both interface and abstract class in the design so the user can
use either the interface or the abstract class whichever is convenient. Such
an abstract class is known as a convenience class.
MyArrayList
MyList MyAbstractList
MyLinkedList
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
5
MyList Interface and MyAbstractList Class
«interface»
MyList<E>
+add(e: E) : void Appends a new element at the end of this list.
+add(index: int, e: E) : void Adds a new element at the specified index in this list.
+clear(): void Removes all the elements from this list.
+contains(e: E): boolean Returns true if this list contains the element.
+get(index: int) : E Returns the element from this list at the specified index.
+indexOf(e: E) : int Returns the index of the first matching element in this list.
+isEmpty(): boolean Returns true if this list contains no elements.
+lastIndexOf(e: E) : int Returns the index of the last matching element in this list.
+remove(e: E): boolean Removes the element from this list.
+size(): int Returns the number of elements in this list.
+remove(index: int) : E Removes the element at the specified index and returns the
removed element.
+set(index: int, e: E) : E Sets the element at the specified index and returns the
element you are replacing.
MyAbstractList<E>
#size: int The size of the list.
MyList
#MyAbstractList() Creates a default list.
#MyAbstractList(objects: E[]) Creates a list from an array of objects.
+add(e: E) : void Implements the add method.
+isEmpty(): boolean Implements the isEmpty method. MyAbstractList
+size(): int Implements the size method.
+remove(e: E): boolean Implements the remove method.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
6
Array Lists
Array is a fixed-size data structure. Once an array is
created, its size cannot be changed.
Nevertheless, you can still use arrays to implement
dynamic data structures.
The trick is to create a new larger array to replace the current array
if the current array cannot hold new elements in the list.
1. Initially, an array, say data of Object[] type, is created with a default size.
2. When inserting a new element into the array, first ensure there is enough
room in the array.
3. If not, create a new array with the size as twice as the current one.
4. Copy the elements from the current array to the new array. The new array
now becomes the current array.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
7
Array List Animation
http://liveexample.pearsoncmg.com/liang/
animation/web/ArrayList.html
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
8
Insertion
Before inserting a new element at a specified
index, shift all the elements after the index to the
right and increase the list size by 1.
data.length -1
e Insertion point …shift…
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
9
Deletion
To remove an element at a specified index, shift
all the elements after the index to the left by one
position and decrease the list size by 1.
data.length -1
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
10
Implementing MyArrayList
MyAbstractList<E>
MyArrayList<E>
-data: E[]
MyArrayList TestMyArrayList
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
11
Linked Lists
Since MyArrayList is implemented using an array, the methods;
– get(int index) and
– set(int index, Object o), and
– add(Object o)
for accessing, and modifying an element through an index, and
adding an element at the end of the list are efficient.
However, the methods;
– add(int index, Object o),
– and remove(int index)
are inefficient because they require shifting potentially a large
number of elements.
You can use a linked data structure to implement a list to improve
efficiency for adding and removing an element anywhere in a list.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
12
Linked List Animation
http://liveexample.pearsoncmg.com/liang/
animation/web/LinkedList.html
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
13
Nodes in Linked Lists
A linked list consists of nodes.
– Each node contains an element, and each node is linked to its
next neighbor.
– Thus a node can be defined as a class, as follows:
class Node<E> {
E element;
Node next;
public Node(E o) {
element = o;
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
14
Adding Three Nodes
The variable head refers to the first node in the list, and
the variable tail refers to the last node in the list.
– If the list is empty, both are null.
For example, you can create three nodes to store three
strings in a list, as follows:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
15
Adding Three Nodes, cont.
Step 2: Create the first node and insert it to the list:
next: null
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
16
Adding Three Nodes, cont.
Step 3: Create the second node and insert it to the
list:
tail
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
17
Adding Three Nodes, cont.
Step 4: Create the third node and insert it to the list:
tail
tail
tail = tail.next;
head "Chicago" "Denver" "Dallas"
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
18
Traversing All Elements in the List
Each node contains the element and a data field named
next that points to the next element.
If a node is the last one in the list, its pointer data field
next contains the value null.
– You can use this property to detect the last node.
For example, you may write the following loop to
traverse all the nodes in the list.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
19
MyLinkedList
MyAbstractList<E>
m 1
Node<E> MyLinkedList<E>
element: E
next: Node<E> -head: Node<E>
-tail: Node<E>
1
MyLinkedList TestMyLinkedList
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
20
Implementing addFirst(E o)
public void addFirst(E o) {
Node<E> newNode = new Node<E>(o);
newNode.next = head;
head = newNode;
size++;
if (tail == null)
tail = head; head tail
} …
e0 ei ei+1 … ek
next next next null
A new node
to be inserted
here o
next
(a) Before a new node is inserted.
head tail
e0 … ei ei+1 …
o ek
next next next next null
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
21
Implementing addLast(E o)
public void addLast(E o) {
if (tail == null) {
head = tail = new Node<E>(element);
}
else {
tail.next = new Node(element);
tail = tail.next;
}
size++; head tail
}
e0 … ei ei+1 … ek
next next next null
A new node o
to be inserted
(a) Before a new node is inserted. null
here
head tail
e0 … ei ei+1 … ek o
next next next next null
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
22
Implementing add(int index, E o)
public void add(int index, E o) {
if (index == 0) addFirst(o);
else if (index >= size) addLast(o);
else {
Node<E> current = head; head current temp tail
for (int i = 1; i < index; i++) …
e0 ei ei+1 …
current = current.next; ek
next next next
Node<E> temp = current.next; null
A new node
is inserted in e
the list (b) After a new node is inserted.
next
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
23
Implementing removeFirst()
public E removeFirst() {
if (size == 0) return null;
else {
Node<E> temp = head;
head = head.next;
size--; head tail
if (head == null) tail = null;
return temp.element; e0 e1 … ei ei+1 … ek
}
Delete this node
(a) Before the node is deleted.
head tail
e1 … ei ei+1 … ek
next next next null
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
24
public E removeLast() {
if (size == 0) return null; Implementing
else if (size == 1)
{
Node<E> temp = head;
removeLast()
head = tail = null;
size = 0; head current tail
return temp.element;
} e0 e1 … ek-2 ek-1 ek
else next next next next null
{
Node<E> current = head;
for (int i = 0; i < size - 2; i++) (a) Before the node is deleted. Delete this node
current = current.next;
Node temp = tail; head tail
tail = current;
tail.next = null; e0 e1 … ek-2 ek-1
size--; next next next null
return temp.element;
} (b) After the last node is deleted
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
25
Implementing remove(int index)
public E remove(int index) {
if (index < 0 || index >= size) return null;
else if (index == 0) return removeFirst();
else if (index == size - 1) return removeLast();
else {
Node<E> previous = head;
for (int i = 1; i < index; i++) {
previous = previous.next; head previous current current.next tail
} …
element element element element … element
Node<E> current = previous.next;
next next next next null
previous.next = current.next;
size--;
Node to be deleted
return current.element;
(a) Before the node is deleted.
}
current.next tail
} head previous
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
26
Time Complexity for ArrayList and LinkedList
Methods MyArrayList/ArrayList MyLinkedList/LinkedList
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
27
Circular Linked Lists
A circular, singly linked list is like a singly
linked list, except that the pointer of the last
node points back to the first node.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
28
Doubly Linked Lists
A doubly linked list contains the nodes with two
pointers.
– One points to the next node and the other points to the
previous node.
– These two pointers are conveniently called a forward pointer
and a backward pointer. So, a doubly linked list can be
traversed forward and backward.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
29
Circular Doubly Linked Lists
A circular, doubly linked list is doubly linked list, ….
– except that …
the forward pointer of the last node points to the first node and
the backward pointer of the first pointer points to the last node.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
30