linked list data structure

65
2015, Marcus Biel, http://www.marcus-biel.com/ Linked List Data Structure Marcus Biel, Software Craftsman http://www.marcus-biel.com

Upload: marcus-biel

Post on 15-Jan-2017

408 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Linked ListData Structure

Marcus Biel, Software Craftsmanhttp://www.marcus-biel.com

Page 2: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Terminology

First of all, let’s have a look at the term “Linked List”. Why is Linked List actually called Linked List?

Page 3: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

TerminologyLink

For the term “Link” - as an analogy - think of a weblink – when you click on it, it brings you from one website to the

next.

Page 4: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

TerminologyList

A list is a collection of related objects. Think of a shopping list - it contains every item that you plan to buy.

Page 5: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

TerminologyLinked List

So a Linked List is a collection of related objects, where a link will bring you from one item to the next item.

Page 6: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

TerminologyLinked List

Technically, in our Linked List, an item or entry of the list…

Page 7: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Node

… is stored in a so called “Node”.

Page 8: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

23

Node

In our simple example, this entry is the number twenty three.

Page 9: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Node23

Each node has a link or pointer to the next element of the list, the next node.

Page 10: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23

For every item that we want to add to the Linked List, we create a node and store the item in it.

Page 11: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23

The first item of the list is usually called the head, the last item of the list the tail of the list.

Page 12: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 3

For every new item, we create a new node…

Page 13: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 93

… and link to it from the last element of the list.

Page 14: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 93 42

So a Linked List is a dynamic data structure, that can hold any number of elements, as long as enough memory is available.

Page 15: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List

next ( )

23 93 42

After the list is created, to navigate through the list we call a function like next()

which uses the link to go from one item to the next.

Page 16: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 93 42

This type of a Linked List is called a Singly Linked List because the links only go in one direction -

Page 17: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 93 42

from the head of the list -

Page 18: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 93 42

- to the tail.

Page 19: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 93 42

So to navigate to the previous element, for example from the element 42 -

Page 20: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 93 42

to the element 9

Page 21: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 93 42

you have to go back to the head of the list -

Page 22: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List

next ( )

23 93 42

and you have to call the function next() -

Page 23: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 93 42

on every single element -

Page 24: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List

next ( )

23 93 42

until you reach the element 9.

Page 25: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 93 42

If the list contains a lot of elements, this may take some time.

Page 26: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 93 42

Inserting an element after the current element is relatively easy with a Singly Linked List.

Page 27: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List

17

23 93 42

You create a new Node containing the new element.

Page 28: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List

17

23 93 42

You link to the new element „17“ from the current element „9”.

Page 29: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List

17

23 93 42

You add a link pointing from the new “17” element to the existing “42” element.

Page 30: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 93 17 42

And you’r done.

Page 31: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 93 42

Inserting the same element BEFORE the current element is possible in a Singly Linked List, but usually not very efficient.

Page 32: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 93 42

It will require to navigate to the previous element, starting from the beginning of the list, as I showed you before.

Page 33: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Singly Linked List23 93 42

Removing an element from a Singly Linked List has the same issue –

it is possible, but generally not very efficient.

Page 34: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Node23

These operations get much easier, when you add a second link to each node,

pointing to the previous element.

Page 35: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Node23

This way you can navigate in both directions of the list.

Page 36: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Node23

However the extra link comes at a cost, of extra memory, as well as time to build the more complex structure.

Page 37: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Node23

If this overhead is justified, I cannot generally answer, as it will differ for each use case.

If performance is an issue, I advise you to test different options.

Page 38: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Doubly Linked List

23

so a Linked List that contains nodes that provide a link to the next and the previous node is called a “Doubly Linked List”.

Page 39: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Doubly Linked List

23 3

For every element that you add to a Doubly Linked List, you need to create two links,

so creating a Doubly Linked List is somewhat more complicated.

Page 40: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Doubly Linked List

23 3 9

Navigation in both directions in a Doubly Linked List is easier, but at the cost of a more complex structure.

Page 41: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Doubly Linked List

23 3 9 42

Based on the two way link structure…

Page 42: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Doubly Linked List

23 3 9 42

17

adding or removing an element before or

after the current element is relatively easy.

Page 43: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Doubly Linked List

23 3 9 42

17

Here we will add the element “17” before the current element “9”.

Page 44: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Doubly Linked List

23 3 9 42

17

The back link from element “9” to element “3” is removed…

Page 45: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Doubly Linked List

23 3 9 42

17

and replaced by a back link to the new element “17”.

Page 46: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Doubly Linked List

23 3 9 42

17

From the new element “17” we link to the next element “9”.

Page 47: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Doubly Linked List

23 3 9 42

17

…next, we place a back link from 17 to 3…

Page 48: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Doubly Linked List

23 3 9 42

17

The old link from element 3 to 9 is replaced by a link from 3 to 17.

Page 49: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Doubly Linked List

23 3 17 9 42

Removing an element from a Doubly Linked List has the same steps

as inserting an element to the list, just in reverse order.

Page 50: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

public class Node<E> { private E item; private Node<E> next; public Node(E element, Node<E> next) { this.item = element; this.next = next; }

public E item() { return item;}

public Node<E> next() { return next;}

[…]}

Node

Let‘s look at a code excerpt of a Node in a Singly Linked List. So you have a method to retrieve the item of a Node,

and a method to go to the next Node.

Page 51: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Nodepublic class Node<E> { private E item; private Node<E> previous; private Node<E> next;

public Node(Node<E> previous, E element, Node<E> next) { this.item = element; this.next = next; this.previous = previous; }

public E item() { return item;}public Node<E> previous() { return previous;}public Node<E> next() { return next;}

[…]

}

The Node of a Doubly Linked List is very similar, but a bit more complex.

Additionally, you have a reference to the previous Node.

Page 52: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

public class LinkedList<E> { private Node<E> currentNode; private Node<E> head;

public E get(int index) {…} public boolean add(E e) {…}

public E remove(int index) {…}

[…]}

LinkedList

A LinkedList usually contains a link to the head of the List. The methods of the Node class

will be used to navigate through the list.

Page 53: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

public class LinkedList<E> { private Node<E> currentNode; private Node<E> head;

public E get(int index) {…} public boolean add(E e) {…}

public E remove(int index) {…}

[…]}

LinkedList

The concrete implementation of methods to get, add or remove an element from the list,

depends on the type of the node, but such an implementation detail is usually hidden from a user of the list.

Page 54: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

public class LinkedList<E> { private Node<E> currentNode; private Node<E> head; private Node<E> tail; public E get(int index) {…}

public boolean add(E e) {…} public E remove(int index) {…}

[…]}

LinkedList

Besides the direct link to the current node of the list and the head of the list,

a Linked List may also provide a direct link to the tail of the list. This is common for a Doubly Linked List,

but may also be beneficial for a Singly Linked List.

Page 55: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Application ScenariosListQueueStackDouble Ended Queue (Deque)

Let’s have a look at different application scenarios for a Linked List. A Linked List can be used to implement data structures like

List, Queue, Stack or Double Ended Queue.

Page 56: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Application ScenariosListQueueStackDouble Ended Queue (Deque)

When comparing implementations based on a Singly- or a Doubly Linked List,

there is no overall winner.

Page 57: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Application ScenariosListQueueStackDouble Ended Queue (Deque)

Both have advantages and disadvantages that make them useful for different application scenarios.

Page 58: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Application ScenariosListQueueStackDouble Ended Queue (Deque)

Using the example of List, Queue, Stack and Double Ended Queue we will in each case decide for a

Singly- or a Doubly Linked based implementation.

Page 59: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

Application ScenariosListQueueStackDouble Ended Queue (Deque)

An implementation with other data structures, for example an array,

is also possible, but that’s a different story.

Page 60: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

ListQueueStackDouble Ended Queue (Deque)

Application Scenarios

A list usually requires random access to any element of the list, so I would recommend

an implementation based on a Doubly Linked List.

Page 61: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

ListQueueStackDouble Ended Queue (Deque)

Application Scenarios

In a so called “first in first out” Queue, new elements are inserted at the tail of the queue and

removed from the head of the queue.

Page 62: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

ListQueueStackDouble Ended Queue (Deque)

Application Scenarios

Random access is not required, so I would recommend

an implementation based on a Singly Linked List.

Page 63: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

ListQueueStackDouble Ended Queue (Deque)

Application Scenarios

A Stack provides a so called “Last in First out” order. Elements are added and removed from the head of the Stack,

which makes it even more simple then a Queue.

Page 64: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

ListQueueStackDouble Ended Queue (Deque)

Application Scenarios

Therefore a Singly Linked List is usually sufficient.

Page 65: Linked List data structure

2015, Marcus Biel, http://www.marcus-biel.com/

ListQueueStackDouble Ended Queue (Deque)

Application Scenarios

A Double Ended Queue or Deque is a very dynamic data structure. It allows to access, add or remove elements from both ends.

Therefore I would use a Doubly Linked List to implement a Deque.