copyright © 2013 by john wiley & sons. all rights reserved. how to create linked lists from...

30
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part and 1 Summary slide We saw how to USE a Linked List in a previous week

Upload: brooklyn-still

Post on 01-Apr-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Copyright © 2013 by John Wiley & Sons. All rights reserved.

HOW TO CREATE LINKED LISTS FROM SCRATCH

CHAPTER

Slides by Rick Giles

16

Only Linked List Part and 1 Summary slide

Only Linked List Part and 1 Summary slide

We saw how to USE a Linked

List in a previous week

We saw how to USE a Linked

List in a previous week

Page 2: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Chapter Goals To understand the implementation of linked lists

To analyze the efficiency of fundamental operations of lists

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 2

Page 3: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Contents Implementing Linked Lists

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 3

Remember Data Structures has traditionally been taught by only

considering how to create Linked Lists etc from scratch/first

principles – we are doing both.

Remember Data Structures has traditionally been taught by only

considering how to create Linked Lists etc from scratch/first

principles – we are doing both.

Page 4: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

16.1 Implementing Linked Lists Previous chapter: Java library LinkedList class

Now, we will look at the implementation of a simplified version of this class

It will show you how the list operations manipulate the links as the list is modified

To keep it simple, we will implement a singly linked list Class will supply direct access only to the first list element, not the

last one

Our list will not use a type parameter Store raw Object values and insert casts when retrieving them

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 4

Page 5: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

The Node Class (1) Node: Stores an object and a reference to the next node

Methods of linked list class and iterator class have frequent access to the Node instance variables

To make it easier to use: We do not make the instance variables private

We make Node a private inner class of LinkedList

It is safe to leave the instance variables public

None of the list methods returns a Node object

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 5

Page 6: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

The Node Class (2)public class LinkedList { ... private class Node { public Object data; public Node next; } }

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 6

Page 7: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

The Node Class (3) LinkedList class

Holds a reference first to the first node

Has a method to get the first element

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 7

public class LinkedList { private Node first; ... public LinkedList() { first = null; } public Object getFirst() { if (first == null) throw new NoSuchElementException(); return first.data; } }

Page 8: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Adding a New First Element (1)

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 8

When a new node is added to the list It becomes the head of the list

The old list head becomes its next node

Page 9: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Adding a New First Element (2)

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 9

public void addFirst(Object obj) { Node newNode = new Node(); newNode.data = obj; newNode.next = first; first = newNode; }

Page 10: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Adding a New First Element (3)

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 10

public void addFirst(Object obj) { Node newNode = new Node(); newNode.data = obj; newNode.next = first; first = newNode; }

Page 11: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Removing the First Element (1)

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 11

When the first element is removed The data of the first node are saved and later returned as

the method result

The successor of the first node becomes the first node of the shorter list

The old node will be garbage collected when there are no further references to it

Page 12: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Removing the First Element (2)

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 12

public Object removeFirst() { if (first == null) throw new NoSuchElementException(); Object obj = first.data; first = first.next; return obj; }

Page 13: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Removing the First Element (3)

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 13

public Object removeFirst() { if (first == null) throw new NoSuchElementException(); Object obj = first.data; first = first.next; return obj; }

Page 14: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Efficiency of Linked List Operations (1)

To get the kth element of a linked list, start at the beginning of the list and advance the iterator k times O(n) for a list with n elements

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 30

Page 15: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Efficiency of Linked List Operations (2)

To add an element at the end of the list, need to advance to the end in O(n) time, followed by O(1) to add the element

Can improve this performance by adding a reference to the last node of the linked list:

public class LinkedList{ private Node first; private Node last; . . .}

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 31

Page 16: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Efficiency of Linked List Operations (3)

Must update last reference when the last node changes, as elements are added or removed

Code for addLast method with this reference is very similar to addFirst method, and can be implemented as a O(1) operation, as in the Java library LinkedList class

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 32

Page 17: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Efficiency of Linked List Operations (4)

To remove the last element, need a reference to the next-to-last element in order to set its next reference to null

It takes n - 1 operations to obtain it, starting at beginning of the list O(n) operation to remove an element from the back of the list

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 33

Page 18: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Efficiency of Linked List Operations (5)

Can do better at removing the last element with a doubly-linked list where each node also has a reference to the previous node, as in the Java library LinkedList class:

public class LinkedList{ . . . class Node { public Object data; public Node next; public Node previous; }}

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 34

Page 19: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Efficiency of Linked List Operations (6)

Removal of the last element takes a constant number of steps, O(1):

Node beforeLast = last.previous;beforeLast.next = null;last = beforeLast;

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 35

Page 20: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

Efficiency of Linked List Operations (7)

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 36

Page 21: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

LinkedList.java

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 37

Continued

Page 22: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

LinkedList.java (cont.)

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 38

Continued

Page 23: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

LinkedList.java (cont.)

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 39

Continued

Page 24: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

LinkedList.java (cont.)

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 40

Continued

Page 25: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

LinkedList.java (cont.)

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 41

Continued

Page 26: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

LinkedList.java (cont.)

Copyright © 2013 by John Wiley & Sons. All rights reserved.

Page 42

Continued

Page 27: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

LinkedList.java (cont.)

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 43

Continued

Page 28: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

ListIterator.java

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 44

Continued

Page 29: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

ListIterator.java (cont.)

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 45

Page 30: Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part

SummaryLinked Lists A linked list object holds a reference to the first node, and each node holds a reference to the next node.

When adding or removing the first element, the reference to the first node must be updated.

A list iterator object has a reference to the last visited node.

To advance an iterator, update the position and remember the old position for the remove method.

In a doubly-linked list, accessing an element is an O(n) operation; adding and removing an element is O(1) .

Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 46