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.

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

Chapter 25 Lists, Stacks, Queues, and Priority Queues
AP STUDY SESSION 2.
1
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
UNITED NATIONS Shipment Details Report – January 2006.
David Burdett May 11, 2004 Package Binding for WS CDL.
Introduction to Computation and Problem
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Local Customization Chapter 2. Local Customization 2-2 Objectives Customization Considerations Types of Data Elements Location for Locally Defined Data.
Process a Customer Chapter 2. Process a Customer 2-2 Objectives Understand what defines a Customer Learn how to check for an existing Customer Learn how.
Custom Statutory Programs Chapter 3. Customary Statutory Programs and Titles 3-2 Objectives Add Local Statutory Programs Create Customer Application For.
Custom Services and Training Provider Details Chapter 4.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt BlendsDigraphsShort.
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
Break Time Remaining 10:00.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
Chapter 17 Linked Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of.
Linked Lists Chapter 4.
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
ITEC200 Week04 Lists and the Collection Interface.
Data Structures Using C++
Double-Linked Lists and Circular Lists
1 The Blue Café by Chris Rea My world is miles of endless roads.
Bright Futures Guidelines Priorities and Screening Tables
EIS Bridge Tool and Staging Tables September 1, 2009 Instructor: Way Poteat Slide: 1.
Bellwork Do the following problem on a ½ sheet of paper and turn in.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
The List ADT Textbook Sections
Copyright © 2013, 2009, 2006 Pearson Education, Inc. 1 Section 5.5 Dividing Polynomials Copyright © 2013, 2009, 2006 Pearson Education, Inc. 1.
Sample Service Screenshots Enterprise Cloud Service 11.3.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
Adding Up In Chunks.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
: 3 00.
5 minutes.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Analyzing Genes and Genomes
Speak Up for Safety Dr. Susan Strauss Harassment & Bullying Consultant November 9, 2012.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Converting a Fraction to %
Clock will move after 1 minute
PSSA Preparation.
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
Physics for Scientists & Engineers, 3rd Edition
Energy Generation in Mitochondria and Chlorplasts
Murach’s OS/390 and z/OS JCLChapter 16, Slide 1 © 2002, Mike Murach & Associates, Inc.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 15 – An Introduction to Data Structures.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Presentation 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 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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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