Lect10 Linked Lists
Lect10 Linked Lists
Motivation
1. Use of a List
Motivation
List is one of the most basic types of data collection
For example, list of groceries, list of modules, list of friends,
etc.
In general, we keep items of the same type (class) in one list
Typical Operations on a data collection
Add data
Remove data
Query data
The details of the operations vary from application to
application. The overall theme is the management of data
The next slide on the basic list interface does not have
all the above operations… we will slowly build up
these operations in list beyond the basic list.
[CS1020 Lecture 10: List ADT & Linked Lists]
8
1. Use of a List
ADT of a List (2/3)
import java.util.*; ListInterface.java
Contractual
obligations: Java Arrays
List ADT
1.Create empty
list
2.Determine … Linked Lists
3.Add an item
To be discussed
…
in section 3:
Basic Linked List
ADT Implementations
Fixed-size list
2. List Implementation: Array (1/9)
This is a straight-forward approach
Use Java array of a sequence of n elements
num_nodes arr : array[0..m] of locations
0 1 2 n-1 m
Legend:
implements
8 a 0 a 1 a 2 a 3 a 4 a 5 a6 a 7
8 a 0 a 1 a 2 a 3 a 4 a 5 a6 a 7
[CS1020 Lecture 10: List ADT & Linked Lists]
18
2. Analysis of Array Impln of List (8/9)
Question: Time Efficiency?
Retrieval: getFirst()
Always fast with 1 read operation
Insertion: addFirst(E item)
Shifting of all n items – bad!
Insertion: add(int index, E item)
Inserting into the specified position (not shown in ListUsingArray.java)
Best case: No shifting of items (add to the last place)
Worst case: Shifting of all items (add to the first place)
Deletion: removeFirst(E item)
Shifting of all n items – bad!
Deletion: remove(int index)
Delete the item at the specified position (not shown in
ListUsingArray.java)
Best case: No shifting of items (delete the last item)
Worst case: Shifting of all items (delete the first item)
[CS1020 Lecture 10: List ADT & Linked Lists]
19
2. Analysis of Array Impln of List (9/9)
Question: What is the Space Efficiency?
Size of array collection limited by MAXSIZE
Problems
We don’t always know the maximum size ahead of time
If MAXSIZE is too liberal, unused space is wasted
If MAXSIZE is too conservative, easy to run out of space
Variable-size list
3.1 List Implementation: Linked List (1/3)
Recap when using an array...
X, A, B are elements of an array
Unused spaces
Y is new element to be added
X A B
I want to add
Y after A. I want to
remove A.
Y
X A B
I want to add
Y after A.
Y
?
X A B
I want to
Node A becomes a
remove A ….
garbage. To be removed
during garbage collection.
ai ai+1
…
String
An instance (object) of a class only comes into existence
(constructed) when the new operator is applied
A reference variable only contains a reference or pointer to an object.
[CS1020 Lecture 10: List ADT & Linked Lists]
26
3.2 Linked List Approach (3/4)
Recap: Object References (2/2)
Look at it in more details:
w = y; Integer
if (w == y)
System.out.println("2. w == y");
Output:
[CS1020 Lecture 10: List ADT & Linked Lists]
27
3.2 Linked List Approach (4/4)
Quiz: Which is the right representation of e?
class Employee {
private String name; Employee e = new Employee("Alan", 2000);
private int salary;
// etc.
}
(A) e (B) e
Alan 2000 Alan 2000
(C) e (D) e
2000
[CS1020 Lecture 10: List ADT & Linked Lists]
28
3.3 ListNode (using generic)
ListNode.java
class ListNode <E> {
/* data attributes */
element next
private E element;
private ListNode <E> next;
/* constructors */
public ListNode(E item) { this(item, null); }
public ListNode(E item, ListNode <E> n) {
element = item;
next = n;
}
/* get the next ListNode */
public ListNode <E> getNext() { return next; }
/* get the element of the ListNode */
public E getElement() { return element; }
/* set the next reference */
public void setNext(ListNode <E> n) { next = n };
}
Mark this slide – You may need to refer to it later
when we study the different variants of linked list.
[CS1020 Lecture 10: List ADT & Linked Lists]
29
3.4 Forming a Linked List (1/3)
For a sequence of 4 items < a0, a1, a2, a3 >
head
represents null
a0 a1 a2 a3
a0 a1 a2 a3
[CS1020 Lecture 10: List ADT & Linked Lists]
31
3.4 Forming a Linked List (3/3)
Alternatively we can form the linked list as follows:
For a sequence of 4 items < a0, a1, a2, a3 >, we can build
as follows:
LinkedList <String> list = new LinkedList <String>();
list.addFirst(“a3”);
list.addFirst(“a2”); I don’t care how
list.addFirst(“a1”); addFirst() is
list implemented
list.addFirst(“a0”);
Is this better than the
head code in previous slide?
a0 a1 a2 a3
0 item head
num_nodes
head
num_nodes
0 99 1
0
1 2
[CS1020 Lecture 10: List ADT & Linked Lists]
35
3.5 Basic Linked List (4/7)
The removeFirst() method
Case Before: list After: list.removeFirst()
0 item head num_nodes
0 can’t remove
ln
1 item head num_nodes head num_nodes
1 0
1
1 1
System.out.println("Testing removal");
list.removeFirst();
list.print();
if (list.contains("aaa"))
list.addFirst("xxxx");
list.print();
}
}
[CS1020 Lecture 10: List ADT & Linked Lists]
38
3.5 Test Basic Linked List #2 (7/7)
Example use #2
TestBasicLinkedList2.java
import java.util.*;
list.addFirst(34);
list.addFirst(12);
list.addFirst(9);
list.print();
System.out.println("Testing removal");
list.removeFirst();
list.print();
}
}
[CS1020 Lecture 10: List ADT & Linked Lists]
39
4 More Linked Lists
+ isEmpty()
+ size()
+ getFirst()
TailedLinkedList + contains(E item)
+ addFirst(E item)
+ removeFirst()
- head
+ print()
- tail + getHead()
- num_nodes + addAfter(ListNode <E> curr, E item)
+ removeAfter(ListNode <E> curr)
+ remove( E item)
item
head
num_nodes
4
5 a0 a1 a2 a3
temp
nextPtr
head a2
num_nodes
4
3 a0 a1 a2 a3
temp
head a0
num_nodes
4
3 a0 a1 a2 a3
}
}
item
prev curr a2
head
num_nodes
4
3 a0 a1 a2 a3
System.out.println();
System.out.println("Part 2");
ListNode <String> current = list.getHead();
list.addAfter(current, "xxx");
list.addAfter(current, "yyy");
list.print();
[CS1020 Lecture 10: List ADT & Linked Lists]
51
4.1 Test Enhanced Linked List (11/11)
// (continue from previous slide) TestEnhancedLinkedList.java
System.out.println();
System.out.println("Part 3");
current = list.getHead();
if (current != null) {
current = current.getNext();
list.removeAfter(current);
}
list.print();
System.out.println();
System.out.println("Part 4");
list.removeAfter(null);
list.print();
}
}
[CS1020 Lecture 10: List ADT & Linked Lists]
52
4. Linked Lists: Variants
OVERVIEW!
BasicLinkedList implements <<interface>>
ListInterface
- head
- num_nodes
+ isEmpty()
+ size()
ListNode + getFirst()
+ contains(E item)
has-a + addFirst(E item)
- element EnhancedLinkedList
- next + removeFirst()
+ print()
+ getNext() - head
+ getElement() - num_nodes
+ setNext(ListNode <<interface>>
EnhancedListInterface
<E> curr)
+ isEmpty()
+ size()
+ getFirst()
TailedLinkedList + contains(E item)
+ addFirst(E item)
+ removeFirst()
- head
+ print()
- tail + getHead()
- num_nodes + addAfter(ListNode <E> curr, E item)
+ removeAfter(ListNode <E> curr)
+ remove( E item)
tail
head
num_nodes
4 a0 a1 a2 a3
a b … g y y
New node New node
Case 1A Case 1B
current != null; current != tail current != null; current == tail
current current tail
… p q … … p q
y y
Case 2A Case 2B
current == null; tail != null current == null; tail == null
head current head current
tail
a b …
y
y New node
New node
System.out.println("Part 1");
list.addFirst("aaa");
list.addFirst("bbb");
list.addFirst("ccc");
list.print();
System.out.println("Part 2");
list.addLast("xxx");
list.print();
System.out.println("Part 3");
list.removeAfter(null);
list.print();
}
}
[CS1020 Lecture 10: List ADT & Linked Lists]
63
4. Linked Lists: Variants
OVERVIEW!
BasicLinkedList implements <<interface>>
ListInterface
- head
- num_nodes
+ isEmpty()
+ size()
ListNode + getFirst()
+ contains(E item)
has-a + addFirst(E item)
- element EnhancedLinkedList
- next + removeFirst()
+ print()
+ getNext() - head
+ getElement() - num_nodes
+ setNext(ListNode <<interface>>
EnhancedListInterface
<E> curr)
+ isEmpty()
+ size()
+ getFirst()
Difficulty: (Boundary cases) + contains(E item)
TailedLinkedList
Take care of all cases of update + addFirst(E item)
0 element - head
+ removeFirst()
1 element + print()
- tail + getHead()
2 elements - num_nodes + addAfter(ListNode <E> curr, E item)
3 or more elements, etc. + removeAfter(ListNode <E> curr)
+ remove( E item)
tail
head
num_nodes
4 a0 a1 a2 a3
prev next
x2
node
head tail
num_nodes
4 x1 x2 x3 x4
[CS1020 Lecture 10: List ADT & Linked Lists]
76
Why “reinvent the wheel”?
In a data structures course, students are often
asked to implement well-known data structures.
A question we sometimes hear from students:
“Since there is the API, why do we need to learn to
write our own code to implement a data structure
like linked list?”
Writing the code allows you to gain an indepth
understanding of the data structures and their
operations
The understanding will allow you to appreciate their
complexity analysis (to be covered later) and use
the API effectively
[CS1020 Lecture 10: List ADT & Linked Lists]
77
7 Summary (1/2)
We learn to create our own data structure
In creating our own data structure, we face 3
difficulties:
1. Re-use of codes (inheritance confusion)
2. Manipulation of pointers/references (The sequence of
statements is important! With the wrong sequence, the
result will be wrong.)
3. Careful with all the boundary cases
Drawings are very helpful in understanding the cases
(point 3), which then can help in knowing what can be
used/manipulated (points 1 and 2)