Download presentation
Presentation is loading. Please wait.
Published byElla Baldwin Modified over 9 years ago
1
Linked Lists
2
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?
3
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
4
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?
5
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
6
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)
7
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)
8
RHS – SOC 8 Arrays revisited OperationArray Random accessO(1) Sequential accessO(1) Insertion – startO(n) Insertion – middleO(n) Insertion – endO(1) Deletion – startO(n) Deletion – middleO(n) Deletion – endO(1)
9
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
10
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 TT
11
RHS – SOC 11 Linked lists In the Java library, the class LinkedList is available T is a type parameter, just as for ArrayList Use LinkedList for creating a linked list of Car objects
12
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()
13
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
14
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..
15
RHS – SOC 15 Linked lists LinkedList personList;... public void removePerson(String name) { ListIterator iter = personList.listIterator(); boolean foundName = false; while ((iter.hasNext()) && (!foundName)) { foundName = iter.next().getName().equals(name); } if (foundName) iter.remove(); }
16
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?
17
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)
18
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)
19
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)
20
RHS – SOC 20 Linked lists OperationArrayLinked List Random accessO(1)O(n) Sequential accessO(1) Insertion – startO(n)O(1) Insertion – middleO(n)O(1) – O(n) Insertion – endO(1) Deletion – startO(n)O(1) Deletion – middleO(n)O(1) – O(n) Deletion – endO(1)
21
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
22
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…
23
RHS – SOC 23 Exercises Review: R15.1, R15.2 Programnming: P15.1, P15.2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.