Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 and 1 Summary slide We saw how to USE a Linked List in a previous week

2 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

3 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.

4 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

5 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

6 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

7 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; } }

8 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

9 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; }

10 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; }

11 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

12 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; }

13 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; }

14 The Iterator Class (1)  We define LinkedListIterator : private inner class of LinkedList  Implements a simplified ListIterator interface  Has access to the first field and private Node class  Clients of LinkedList don’t actually know the name of the iterator class  They only know it is a class that implements the ListIterator interface Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 14

15 The Iterator Class (2) public class LinkedList {... public ListIterator listIterator() { return new LinkedListIterator(); } class LinkedListIterator implements ListIterator { private Node position; private Node previous; private boolean isAfterNext; public LinkedListIterator() { position = null; previous = null; isAfterNext = false; } }... } Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 15

16 Advancing an Iterator (1)  position : Reference to the last visited node  Also, store a reference to the last reference before that  next method: position reference is advanced to position.next  Old position is remembered in previous  If the iterator points before the first element of the list, then the old position is null and position must be set to first Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 16

17 Advancing an Iterator (2) class LinkedListIterator implements ListIterator { … public Object next() { if (!hasNext()) { throw new NoSuchElementException(); } previous = position; // Remember for remove isAfter = true; if (position == null) { position = first; } else { position = position.next; } return position.data; } … } Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 17

18 Advancing an Iterator (3)  The next method should only be called when the iterator is not at the end of the list  The iterator is at the end  if the list is empty ( first == null )  if there is no element after the current position ( position.next == null ) Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 18

19 Advancing an Iterator (4) class LinkedListIterator implements ListIterator { … public boolean hasNext() { if (position == null) { return first != null; } else { return position.next != null; } } … } Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 19

20 Removing an Element (1)  If the element to be removed is the first element, call removeFirst  Otherwise, the node preceding the element to be removed needs to have its next reference updated to skip the removed element  If the previous reference equals position :  This call does not immediately follow a call to next  Throw an IllegalStateException  It is illegal to call remove twice in a row  remove sets the previous reference to position Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 20

21 Removing an Element (2) class LinkedListIterator implements ListIterator { … public void remove() { if (!isAfterNext) { throw new IllegalStateException(); } if (position == first) { removeFirst(); } else { previous.next = position.next; } position = previous; isAfterNext =false; } … } Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 21

22 Removing an Element (3) Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 22

23 Removing an Element (4) class LinkedListIterator implements ListIterator { … public void remove() { if (!isAfterNext) { throw new IllegalStateException(); } if (position == first) { removeFirst(); } else { previous.next = position.next; } position = previous; isAfterNext =false; } … } Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 23

24 Removing an Element (5) Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 24

25 Adding an Element (1) class LinkedListIterator implements ListIterator { … public void add(Object element) { if (position == null) { addFirst(element); position = first; } else { Node newNode = new Node(); newNode.data = element; newNode.next = position.next; position.next = newNode; position = newNode; } isAfterNext = false; } … } Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 25

26 Adding an Element (2) Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 26

27 Adding an Element (3) class LinkedListIterator implements ListIterator { … public void add(Object element) { if (position == null) { addFirst(element); position = first; } else { Node newNode = new Node(); newNode.data = element; newNode.next = position.next; position.next = newNode; position = newNode; } isAfterNext = false; } … } Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 27

28 Adding an Element (4) Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 28

29 Setting an Element to a Different Value  The set method changes the data stored in the previously visited element: public void set(Object element) { if (!isAfterNext) { throw new IllegalStateException(); } position.data = element; } Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 29

30 Efficiency of Linked List Operations (1)  To get the k th 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

31 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

32 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

33 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

34 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

35 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

36 Efficiency of Linked List Operations (7) Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 36

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

38 LinkedList.java (cont.) Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 38 Continued

39 LinkedList.java (cont.) Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 39 Continued

40 LinkedList.java (cont.) Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 40 Continued

41 LinkedList.java (cont.) Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 41 Continued

42 LinkedList.java (cont.) Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 42 Continued

43 LinkedList.java (cont.) Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 43 Continued

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

45 ListIterator.java (cont.) Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 45

46 Summary Linked 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


Download ppt "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."

Similar presentations


Ads by Google