Linked Lists
Download
Report
Transcript Linked Lists
Linked Lists
Linked lists
• We can already store collections of objects in arrays and
array lists – why would we
need other data structures…?
• Arrays are fine, but not perfect
in all respects
• What do we actually do with a
data structure?
RHS – SWC
2
Linked lists
• Operations on data structures:
– Access – inspect the data in an element
– Insertion – add a given element to the
collection
– Deletion – delete a specific element from the
collection
• The efficiency of these operations vary
between different data structures
RHS – SWC
3
Arrays revisited
• Efficiency is here in terms
of run-time complexity of
an operation
• What is the expected runtime complexity for these
operations for an array
structure?
RHS – SWC
4
Arrays revisited
• Access for arrays
– Very efficient!
– We can access any element (so-called
random access) in constant time, using the
index of the element
– We can traverse the elements (so-called
sequential access) such that each traversal
step takes constant time
RHS – SWC
5
Arrays revisited
• Insertion for arrays
– Not so efficient
– Inserting an element at the beginning of the
array requres that all other elements are
moved, i.e O(n)
– Inserting an element in the middle of the array
requres that all elements following the new
element are moved, i.e O(n)
– Inserting an element at the end of the array is
efficient, i.e O(1)
RHS – SWC
6
Arrays revisited
• Deletion for arrays
– Not so efficient
– Deleting an element at the beginning of the
array requres that all other elements are
moved, i.e O(n)
– Deleting an element in the middle of the array
requres that all elements following the
removed element are moved, i.e O(n)
– Deleting an element at the end of the array is
efficient, i.e O(1)
RHS – SWC
7
Arrays revisited
Operation
Array
Random access
Sequential access
Insertion – start
Insertion – middle
O(1)
O(1)
O(n)
O(n)
Insertion – end
Deletion – start
Deletion – middle
O(1)
O(n)
O(n)
Deletion – end
O(1)
RHS – SWC
8
Arrays revisited
• Arrays are brilliant for data access, but
have problems for insertion and deletion
• Good choice in case of
– Heavy access
– Sparse insertion and deletion
• What should we then use in the opposite
case…?
• Linked lists is an alternative
RHS – SWC
9
Linked lists
• A linked list is defined in terms of nodes
• A node for some class T contains:
– An object of class T
– An object reference to a node object
T
T
RHS – SWC
10
Linked lists
• In the Java library, the class
LinkedList<T> is available
• T is a type parameter, just
as for ArrayList<T>
• Use LinkedList<Car> for
creating a linked list of Car
objects
RHS – SWC
11
Linked lists
• What is the interface to LinkedList?
• Methods for accessing, inserting and
deleting elements at the beginning and
end of the list:
void addFirst(T t)
T getFirst()
T removeFirst()
RHS – SWC
void addLast(T t)
T getLast()
T removeLast()
12
Linked lists
• Note that a linked list does not give you
access to elements via an index
• How do we access elements in the middle
of the list (i.e. not the first or last element)?
• We must use a so-called iterator
RHS – SWC
13
Linked lists
• An iterator is a class which helps us iterate
(traverse) through a collection of data, e.g.
a linked list
• An iterator is an abstraction – we do not
need to worry about how the iterator keeps
track of the iteration
• Has a simple interface, with methods like
next, hasNext, etc..
RHS – SWC
14
Linked lists
LinkedList<Person> personList;
...
public void removePerson(String name)
{
ListIterator<Person> iter = personList.listIterator();
boolean foundName = false;
while ((iter.hasNext()) && (!foundName))
{
foundName = iter.next().getName().equals(name);
}
if (foundName)
iter.remove();
}
RHS – SWC
15
Linked lists
• Using a linked list is very different than
using an array
• Operations are done through the iterator,
not the list itself (except for operations on
first or last element)
• What is the run-time complexity for these
operations for a linked list?
RHS – SWC
16
Linked lists
• Access for lists
– Not so efficient
– In order to (randomly) access an element, we
must traverse the list, examining elements in
turn until we reach the element, i.e. O(n)
– We can traverse the elements sequentially,
such that each traversal step takes constant
time, i.e. O(1)
RHS – SWC
17
Linked lists
• Insertion for linked lists
– Fairly efficient
– Inserting an element at the beginning of the
list requires a fixed number of steps, i.e O(1)
– Inserting an element in the middle of the array
requres that we iterate to the desired position,
i.e. O(n), and then insert the element, O(1)
– Inserting an element at the end of the list
requires a fixed number of steps, i.e O(1)
RHS – SWC
18
Linked lists
• Deletion for linked lists
– Fairly efficient
– Deleting an element at the beginning of the
list requires a fixed number of steps, i.e O(1)
– Deleting an element in the middle of the array
requres that we iterate to the element, i.e.
O(n), and then delete the element, O(1)
– Deleting an element at the end of the list
requires a fixed number of steps, i.e O(1)
RHS – SWC
19
Linked lists
Operation
Array
Linked List
Random access
Sequential access
Insertion – start
Insertion – middle
O(1)
O(1)
O(n)
O(n)
O(n)
O(1)
O(1)
O(1) – O(n)
Insertion – end
Deletion – start
Deletion – middle
O(1)
O(n)
O(n)
O(1)
O(1)
O(1) – O(n)
Deletion – end
O(1)
O(1)
RHS – SWC
20
Linked lists
• Linked lists are very good in case of
– Sparse random access
– Heavy sequential access
– Frequent insertion at list start or end
• Not easy to decide whether to use a linked
list or an array structure
• Very important to test using real-life usage
scenarios
RHS – SWC
21
Linked lists
• Final remarks on linked lists in Java:
– Implementation of linked lists does actually
allow access by indices (random access)
– However, this is still quite inefficient!
– Developers have tried to create a common
interface for linked lists and array lists
– Convenient, but beware…
RHS – SWC
22