copyright © 2013 by john wiley & sons. all rights reserved. how to create linked lists from...
TRANSCRIPT
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
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
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.
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
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
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
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; } }
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
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; }
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; }
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
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; }
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; }
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
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
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
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
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
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
Efficiency of Linked List Operations (7)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 36
LinkedList.java
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 37
Continued
LinkedList.java (cont.)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 38
Continued
LinkedList.java (cont.)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 39
Continued
LinkedList.java (cont.)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 40
Continued
LinkedList.java (cont.)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 41
Continued
LinkedList.java (cont.)
Copyright © 2013 by John Wiley & Sons. All rights reserved.
Page 42
Continued
LinkedList.java (cont.)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 43
Continued
ListIterator.java
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 44
Continued
ListIterator.java (cont.)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 45
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