23
Linked Lists

Linked Lists. RHS – SOC 2 Linked lists We can already store collec- tions of objects in arrays and array lists – why would we need other data structures…?

Embed Size (px)

Citation preview

Linked Lists

RHS – SOC 2

Linked lists

• We can already store collec-tions 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 – SOC 3

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 – SOC 4

Arrays revisited

• Efficiency is here in terms of run-time complexity of an operation

• What is the expected run-time complexity for these operations for an array structure?

RHS – SOC 5

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 – SOC 6

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 – SOC 7

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 – SOC 8

Arrays revisited

Operation Array

Random access O(1)

Sequential access O(1)

Insertion – start O(n)

Insertion – middle O(n)

Insertion – end O(1)

Deletion – start O(n)

Deletion – middle O(n)

Deletion – end O(1)

RHS – SOC 9

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 – SOC 10

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 – SOC 11

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 – SOC 12

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) void addLast(T t)

T getFirst() T getLast()

T removeFirst() T removeLast()

RHS – SOC 13

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 – SOC 14

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 – SOC 15

Linked listsLinkedList<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 – SOC 16

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 – SOC 17

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 – SOC 18

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 – SOC 19

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 – SOC 20

Linked lists

Operation Array Linked List

Random access O(1) O(n)

Sequential access O(1) O(1)

Insertion – start O(n) O(1)

Insertion – middle O(n) O(1) – O(n)

Insertion – end O(1) O(1)

Deletion – start O(n) O(1)

Deletion – middle O(n) O(1) – O(n)

Deletion – end O(1) O(1)

RHS – SOC 21

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 – SOC 22

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 – SOC 23

Exercises

• Review: R15.1, R15.2

• Programnming: P15.1, P15.2