1 COP 3540 Data Structures with OOP Chapter 5 - Part 3 Linked Lists Doubly-Linked Lists and Iterators.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Singly linked lists Doubly linked lists
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Linked Lists CSC220 Winter
1/27 COP 3540 Data Structures with OOP Chapter 5 Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
LIST( using dynamic memory allocation). Introduction  We need dynamic Memory Allocation, when the data is dynamic in nature.  That is, the number of.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Linked Lists. Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link (pointer or reference)
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Data Structures Using C++ 2E
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures Using C++ 2E
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Chapter 18 Java Collections Framework
1/26 COP 3540 Data Structures with OOP Chapter 5 - Part 2 Linked Lists Abstract Data Types and Sorted Lists.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Linked Lists © John Urrutia 2013, All Rights Reserved1.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Introduction to Data Structures and Algorithms
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Collections Using Generics in Collections. 2 Chapter Objectives Define the concept and terminology related to collections Explore the basic structure.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
LINKED LISTS.
Linked List First next Data Linked List Link next Data Link next Data Link next Data Link Null.
Chapter 16: Linked Lists.
COP 3538 Data Structures with OOP
Chapter 17 Object-Oriented Data Structures
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 13 Collections.
Dynamic Data Structures and Generics
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Presentation transcript:

1 COP 3540 Data Structures with OOP Chapter 5 - Part 3 Linked Lists Doubly-Linked Lists and Iterators

2 Doubly Linked Lists  How do we track in singly-linked lists? current = current.next;  Must always keep link from ‘last’ link for insert() and delete(), etc….  A given link has its data and a forward link!  Many applications require both forward and rearward traversal of a linked list.  To do this, each link has both forward and rearward (backward) links (pointers).  Thus, from any given link, can go either direction.

3 Code for class Link class Link { public long dData; // data item public Link next; // next link in list public link previous; // previous link in list } Downside: each time you insert() a link, you MUST account for two pointers: one to the next link and one to the rear link. Inserting and Deleting can be very complicated. One MUST keep track of the links and change them in a prescribed order (we shall see).

4 null 22; 2.99 first null 22; 2.99 first null 22; 2.99 first ; 6.99 Conceptual: Doubly-Linked List null These may also be double-ended too (not shown) (Can start at both the beginning and ending of the linked list. Double ends not shown above)

5 null 22; 2.99 first null 22; 2.99 first null 22; 2.99 first Using Memory Addresses – Doubly-Linked List 5A4 6B8 5AC 6B4null 6B8 null 5AC First: find out where the link should be located in list. Search to find. Found. To add the link at 6B8, we had to change the forward link in 5AC to point to the new link (at 6B88), and move 5A4 (former forward link in 5AC) into the forward link of the new link at 6B8. Then, need move backward link in 5A4 to 6B8 and set rear link in 6B8 to 5AC. But sequence is important! I’d fill in links in the new cell first. Remember, you are pointing at 5A4 when you start. So, you have all necessary addresses to do job. Here, we inserted a new first link. First: we will inserted new link at front of list Allocated link is at 5AC. Now insert another link where indicated. The link itself is at 6B A4 6B8 5AC

6 Inserting into a doubly-linked list.  Now, you may need to insert before or insert after a link;  You may have to insert at the beginning and insert at the very end too.  You will almost always have to search to where you will insert a new link.

7 Deleting a link from a doubly-linked list  Must have to search for a hit for the link to be deleted.  May be at front of list  May be last link of list  May be in the middle somewhere in linked list.  Item to be deleted may be not found!!  Your code must account for all of these!

8 Deletion in a doubly-linked list null 22; 2.99 first A4 6B8 5AC 6B4null 6B8 Let’s delete this link A4 6B8 5AC To remove 6B8, you must move the forward link in 6B8 (which is 5A4) to the forward link at 5AC. Thus 5A4 (from 6B8) replaces 6B8 (in 5AC) null 22; A4 6B8 5AC null 6B A4 6B8 5AC 5A4 Now, take care of the rear link in 5A4 (which is 6B8) to the point to what was in the rear link of 6B8 (which is 5AC) null 22; A4 6B8 5AC null 6B A4 6B8 5AC 5A4 5AC Yields: And there is no forward link to 5A4… Yields: And there is no backward link to 5A4…

9 class Link { public long dData; // data item public Link next; // next link in list public Link previous; // previous link in list // public Link(long d) // constructor { dData = d; } // public void displayLink() // display this link { System.out.print(dData + " "); } // } // end class Link

10 class DoublyLinkedApp { public static void main(String[] args) { // make a new list DoublyLinkedList theList = new DoublyLinkedList(); theList.insertFirst(22); // insert at front theList.insertFirst(44); theList.insertFirst(66); theList.insertLast(11); // insert at rear theList.insertLast(33); theList.insertLast(55); theList.displayForward(); // display list forward theList.displayBackward(); // display list backward theList.deleteFirst(); // delete first item Where / how does client get these theList.deleteLast(); // delete last item methods? Does s/he know how theList.deleteKey(11); // delete item with key 11 they are implemented? Who wrote the implementing class? (next slides) theList.displayForward(); // display list forward Note the functionality provided to client! theList.insertAfter(22, 77); // insert 77 after 22 theList.insertAfter(33, 88); // insert 88 after 33 theList.displayForward(); // display list forward } // end main() } // end class DoublyLinkedApp

11 class DoublyLinkedList { private Link first; // ref to first item private Link last; // ref to last item public DoublyLinkedList() // constructor { first = null; // no items on list yet last = null; } public boolean isEmpty() // true if no links { return first==null; } public void insertFirst(long dd) { // insert at front of list. NO SEARCH !! Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, last = newLink; // newLink <-- last else first.previous = newLink; // newLink <-- old first newLink.next = first; // newLink --> old first first = newLink; // first --> newLink } public void insertLast(long dd) { // insert at end of list NO SEARCH !!!! Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, first = newLink; // first --> newLink else { last.next = newLink; // old last --> newLink newLink.previous = last; // old last <-- newLink } last = newLink; // newLink <-- last }// of 5

12 public Link deleteFirst() // delete first link { // (assumes non-empty list) Link temp = first; if(first.next == null) // if only one item last = null; // null <-- last else first.next.previous = null; // null <-- old next first = first.next; // first --> old next return temp; } public Link deleteLast() // delete last link { // (assumes non-empty list) Link temp = last; if(first.next == null) // if only one item first = null; // first --> null else last.previous.next = null; // old previous --> null last = last.previous; // old previous <-- last return temp; } // of 5

13 public boolean insertAfter(long key, long dd) { // (assumes non-empty list) Link current = first; // start at beginning while(current.dData != key) // until match is found, { current = current.next; // move to next link if(current == null) return false; // didn't find it } Link newLink = new Link(dd); // make new link if(current==last) // if last link, This one is your typical insert IF you want to { insert a node after a specific value. newLink.next = null; // newLink --> null Have NOT shown you how to insert into last = newLink; // newLink <-- last a sorted linked list in code. Showed you } earlier (figures) how to insert via figure. else // not last link, { So, assuming values are ascending, you newLink.next = current.next; // newLink --> old next would have to search until your search arg // newLink <-- old next is smaller than the value of the current node! current.next.previous = newLink; } Then “standard” insert. newLink.previous = current; // old current <-- newLink current.next = newLink; // old current --> newLink return true; // found it, did insertion } 3 of 5

14 public Link deleteKey(long key) // delete item w/ given key { // (assumes non-empty list) Link current = first; // start at beginning while(current.dData != key) // until match is found, { current = current.next; // move to next link if(current == null) return null; // didn't find it } if(current==first) // found it; first item? first = current.next; // first --> old next else // not first // old previous --> old next current.previous.next = current.next; if(current==last) // last item? last = current.previous; // old previous <-- last else // not last // old previous <-- old next current.next.previous = current.previous; return current; // return value } 4 of 5

15 // public void displayForward() { System.out.print("List (first-->last): "); Link current = first; // start at beginning while(current != null) // until end of list, { current.displayLink(); // display data current = current.next; // move to next link } System.out.println(""); } // public void displayBackward() { System.out.print("List (last-->first): "); Link current = last; // start at end while(current != null) // until start of list, { current.displayLink(); // display data current = current.previous; // move to previous link } System.out.println(""); } // } // end class DoublyLinkedList 5 of 5

16 PLEASE NOTE THAT MY APPROACH IMPLIES THAT YOU KEEP YOUR LINKED LIST ORDERED. THIS IMPLIES YOU SEARCH IN ORDER TO FIND THE CORRECT PLACE TO ADD (INSERT) SEARCH (FIND) TO FIND LINK TO DELETE AND THEN DELETE A LINK.

17 Iterators  So far, we’ve learned how to search to find, insert, delete, etc.  Also, to do these things at the beginning and end of the list.

18 Accessing elements in the Linked List (web)  Accessing List Elements  There are a few ways to access elements in a list:  Our approach:  Iterator Based: We could abstract node pointers by using objects known as iterators.

19 The Iterator Based Approach To Element Access  This approach uses a special iterator class that encapsulates a node pointer.  One of the key advantages of this is that we can use this to provide consistent semantics to different container types.  Operative concepts: Consistent semantics, and Container types  Let’s examine…

20 Containers and Container Classes  A class used to store data objects is sometimes called a container class.  Typically a container class not only  stores the data but also  provides methods for accessing the data and perhaps also sorting it and performing other complex actions on it.  Different container type: (Containers are also used in graphics applications, where containers may contain other containers as part of the GUI.  Containers (in this sense) may contain frames or panels or other ‘components.’)

21 Collections and Collection Classes  A collection is an object that serves as a repository for other objects.  Generic term - applied to many situations.  Use when discussing an object whose specific role is to provide services to add, remove, and otherwise manage the elements that are ‘contained’ within it.  ArrayList class represents a collection, for it provides methods to add elements to the end of a list or to a particular location in the list based on an index value.  Some collections are homogeneous because they contain all of the same type of object; other collections are heterogeneous because they can contain objects of various types.  It is heterogeneous in that an ArrayList can store object references. P textbook (third edition)

22 Separating Interface from Implementation  Collections can be implemented in many ways.  The underlying data structure that stores the objects can be implemented in using various techniques.  WE know that an ADT is a collection of data and particular operations allowed on that data.  ADT has a  name,  domain of values and  A set of operations.  It is abstract because the operations performed on the data are separated from the underlying implementation; that is, the details of how an ADT stores its data and accomplishes its methods are separate from the concept that it embodies.

23 Representing Data Structures  An array is only one way a list can be represented.  Arrays are fixed size.  An ArrayList class handles the size problem by creating a larger array and copying everything into it when this becomes necessary.  Not necessarily an efficient implementation of a list.  A dynamic data structure implemented using links as references between these objects is more appropriate.  Can be quite efficient to search and modify.  Structures as this are considered dynamic because their size is determined ‘dynamically’ as needed and not by their declaration.

24 Other dynamic list representations  A doubly-linked list.  Each node (link) has references to the next node and also to the previous node. class Node { int info; Node next, prev; } // taken from 2551 textbook.  Another implementation of a linked list could have a header node, which might contain references to the first and last nodes and also perhaps a node count. (makes it easier to add to front and rear or search starting at rear, etc.).  In this context, the header node is not of the same class as the linked list Node. It is a node that contains a couple of references and a count, as in: class ListHeader { int info; // whatever Node front, rear; } // taken from 2551 textbook.  These are simply a different implementations...

25 Other dynamic list representations  Other classic data structures:  Queues  Stacks  Trees – general  Binary trees  Graphs  Directed graphs, and more…

26 Java API Collection Classes  These names (in the API) of the classes in this set generally indicate both the collection type and the underlying implementation.  Examples:  ArrayList  LinkedList – represents collection w/dynamically-linked internal representation  Vector  Stack  Interfaces used to define the collection operations themselves:  List  Set  SortedSet  Map  SortedMap  A set is consistent with its normal interpretation as a collection of elements without duplicates.  A map is a group of elements that can be referenced by a key value  p. 653 COP2551 textbook.

27 Collection API in java.util. - from the Java API link. All Known Implementing Classes: AbstractCollection, AbstractList, AbstractSet, ArrayList, … HashSet, LinkedHashSet, LinkedList, TreeSet, Vector. public interface Collection. A collection represents a group of objects known as its elements. (Remember: collection classes contain objects…) Some collections allow for duplicates and others do not. Some are ordered; some are not. The SDK does not provide any direct implementations of this interface. This interface is typically used to pass collections around and manipulate them where maximum generality is desired.

28 Iterators p. 39 Java - Software Structures textbook  An iterator is an object that provides the means to iterate over a collection.  It provides methods that allow the user to acquire and use each element in a collection in turn.  Most collections provide one or more ways to iterate over their elements.  The Iterator interface: defined in Java standard class API.  Two primary abstract methods in the interface and implemeneted in an iterator object (alluded to above) defined in the interface are:  hasNext() – boolean; returns true if there are more elements in the iteration, and  next – returns the next element in the iteration.  Typically, users interact with the iterator object using hasNext and next methods to access elements in the collection.

29 Iterator – Java API:  Method Summary  boolean hasNext() Returns true if the iteration has more elements hasNext  Object next() Returns the next element in the iteration. Objectnext  void remove() Removes from the underlying collection the last element returned by the iterator (optional operation).remove  Might want to look up public void remove(). Neat. There is no assumption about the order in which an iterator object delivers the elements from the collection. Sometimes the order will be arbitrary; other times, the iterator may follow some particular order that makes sense for that collection. Remember: the implementation of the iterator interface (the iterator object) is our responsibility.

30 Iterators in an Array. (back to our textbook)  In array operations, iterating through an array is simple – by accessing each element and incrementing the index to the next item.  Any integer variable can be used as an iterator for an array.  It is frequently very useful to be able to maintain more than one iterator simultaneously for a single collection of objects: using one (for example) forward iterator and one backward iterator.

31 Array Iterators … moving to List iterators  Two things are required of an iterator:  You need some way to advance the iterator and fetch the next value,,and  You need some way to determine when you have iterated over the entire collection.  For arrays, this is pretty easy, as we implement, typically, as next and a hasNext().

32 Linked List Iterators: Reference in the List Itself  For a linked list, one must access each element to gain the reference (link) to the next item  This can be very time consuming issuing a find() on this list each time and repeatedly searching the linked list!  So, to use an iterator, we will create an iterator object associated with the list.  But in the implementation of an iterator in linked lists, we let the list create the iterator object so the list can pass the iterator its starting address (perhaps others) to enable the iterator to access the linked list.  The simplest iterators have only the two methods hasNext() and next().

33  Introducing Iterators  An iterator is an object that represents a position.  Makes it possible to move around in a container, without exposing the structure of that container.  That is, we don’t know how hasNext() and next() are implemented…  All container iterators support a "fetch" operation that retrieves the data at a given position, as well as comparison and assignment operations.  All iterators are not equal and may well support different operations.  For example, an iterator for a singly linked list supports "forward" movement, but not "backward" movement.  An iterator for an array class supports movement in both directions, and supports constant time addition (random access).

34  There are different types of iterators, with different capabilities: They depend on the type of collection they are iterating through.  Forward iterators support forward movement.  Bidirectional iterators support forward and backward movement.  Hence they are also forward iterators.  Doubly linked lists, and binary trees support bidirectional iterators.  Random access iterators support forward, and backward movement, as well as constant time jumps of a given number of positions.  Array classes support random access iterators. random access iterators are also bidirectional iterators.

35  Iterator: an object that contains references to items in a data structure and uses these references to traverse these structures.  Our approach: declare an iterator associated with a list.  Can declare a number of iterators…  The user can create a list with an iterator object within the list. (ahead)  It can then pass the iterator object data it needs such as the reference to the first element of the list (or anything else!)  By adding a getIterator() method to the list class, the iterator is returned to the client for access.  Consider the code:

36 public static void main (…) { LinkList theList = newLinkList (); // make new object of type LinkedList ListIterator iter1 = theList.getIterator(); // make iterator object iter1 which (we will see) will be passed // the reference the linked list (theList) it is associated with as part of its // constructor. Link alink = iter1.getCurrent(); // Using the iterator, we access a link iter1.nextLink(); // move iter to next link } Note: We used ‘iter1.’ We can have a number of iterators each with different ‘specializations.’ Note: the iterator can be designed to refer anywhere in the list.* (mention ‘filters.’) Remember, an iterator is an object that always points to some link in the list. It’s associated with the list, but its not the same as the list and it is not a link in the list.

37 This is the class we are making an instance of (object) IN the linked list object. class ListIterator() // note this iterator is incomplete… { private Link current; // reference to current link private Link previous; // reference to previous link  private LinkList ourList // reference to ‘parent’ list public void reset() { current = ourList.getFirst(); // current  first previous = null;// previous  null } public void nextLink() // go to next link { previous = current; // set previous to this current = current.next;// set this to next } Note: this iterator has references to the previous and current links. The method, nextLink() provides us with reference to the next link. …// Let’s continue with more… Revised Iterator Class – our own

38 class Link { public long dData; // data item public Link next; // next link in list // public Link(long dd) // constructor { dData = dd; } // public void displayLink() // display yourself { System.out.print(dData + " "); } } // end class Link Link for Singly-Linked List

39 class LinkList { private Link first; // ref to first item on list // public LinkList() // constructor { first = null; } // no items on list yet // public Link getFirst() // returns ref to first link { return first; } // public void setFirst(Link f) // set first to new link { first = f; } // public boolean isEmpty() // true if list is empty { return first==null; } //  public ListIterator getIterator() // return iterator { return new ListIterator(this); // initialized with this list } // public void displayList() { Link current = first; // start at beginning of list while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); } } // end class LinkList Here, we create a List iterator object that points to this linked list! Creates an object of type ListIterator and the object is initialized by ‘this.’

40  class ListIterator { private Link current; // current link private Link previous; // previous link private LinkList ourList; // our linked list // public ListIterator(LinkList list) // constructor  must be passed a reference to an existing Linked List. {// which it is. (this). When this object was created (last slide), it now ourList = list;// points to a specific linked list. reset(); } // public void reset() // start at 'first' { current = ourList.getFirst(); previous = null; } // public boolean atEnd() // true if last link methods needed for this iterator. { return (current.next==null); } // public void nextLink() // go to next link { previous = current; current = current.next; } // public Link getCurrent() // get current link { return current; } // Note how this nextLink() is implemented! Instance variable current ‘holds’ current position in the linked list. So, nextLink() merely moves us along.

41 public void insertAfter(long dd) { // insert after current link Link newLink = new Link(dd); if( ourList.isEmpty() ) // empty list { ourList.setFirst(newLink); current = newLink; } else // not empty { newLink.next = current.next; current.next = newLink; nextLink(); // point to new link } public void insertBefore(long dd) { // insert before current link Link newLink = new Link(dd); methods needed for this iterator if(previous == null) // beginning of list { // (or empty list) newLink.next = ourList.getFirst(); ourList.setFirst(newLink); reset(); } else // not beginning { newLink.next = previous.next; previous.next = newLink; current = newLink; }

42 public long deleteCurrent() // delete item at current { long value = current.dData; if(previous == null) // beginning of list { ourList.setFirst(current.next); reset(); } else // not beginning { previous.next = current.next; if( atEnd() ) reset(); else current = current.next; } return value; } } // end class ListIterator

43  class InterIterApp { public static void main(String[] args) throws IOException { LinkList theList = new LinkList(); // new list ListIterator iter1 = theList.getIterator(); // new iter long value; iter1.insertAfter(20); // insert items iter1.insertAfter(40); iter1.insertAfter(80); iter1.insertBefore(60); while(true) { System.out.print("Enter first letter of show, reset, "); System.out.print("next, get, before, after, delete: "); System.out.flush(); int choice = getChar(); // get user's option switch(choice) { case 's': // show list if( !theList.isEmpty() ) theList.displayList(); //note displayList() is part of linked list – not the iterator. else System.out.println("List is empty"); break; Continuing…. Returns a reference to the linked list and iter, a new object of type ListIterator points to it! Note: theList contains a method named: getIterator() which when invoked returns a pointer to itself!

44 case 'r': // reset (to first) iter1.reset(); break; case 'n': // advance to next item if( !theList.isEmpty() && !iter1.atEnd() ) iter1.nextLink(); else System.out.println("Can't go to next link"); break; case 'g': // get current item if( !theList.isEmpty() ) { value = iter1.getCurrent().dData; System.out.println("Returned " + value); note: all methods are in the iterator. Linked list itself only } has very fundamental methods plus, of course, the getIterator() method which returns a pointer to itself. else Note in here, also we have inserted before current, insert after current, delete current, get current, … System.out.println("List is empty"); break; case 'b': // insert before current System.out.print("Enter value to insert: "); System.out.flush(); value = getInt(); iter1.insertBefore(value); break; case 'a': // insert after current System.out.print("Enter value to insert: "); System.out.flush(); value = getInt(); iter1.insertAfter(value); break; case 'd': // delete current item if( !theList.isEmpty() ) { value = iter1.deleteCurrent(); System.out.println("Deleted " + value); } code missing – (for illustration purposes only. See text for missing source code)

45 Continuing… default: System.out.println("Invalid entry"); } // end switch } // end while } // end main() // public static String getString() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } // public static char getChar() throws IOException { String s = getString(); return s.charAt(0); } // public static int getInt() throws IOException { String s = getString(); return Integer.parseInt(s); } // } // end class InterIterApp

46 We are showing only revised main() routine; everything else is the same as in interIterator.java. Class InterInerApp { public static void main(….) throws IOException { LinkList theList = newLinkList();// new list ListIterator iter1 = theList.getIterator(); // new iterator iter1.insertAfter (21);// insert links iter1.insertAfter (40);// more insert 30, 7, and 45 too theList.displayList();// displays the linked list iter1.reset();// start at first link Link aLink = iter1.getCurrent();// get it if (aLink.dData %3 == 0) iter1.deleteCurrent(); while (!iter1.atEnd()) { iter1.nextLink();// go to next link aLink = iter1.getCurrent();// get link if (aLink.dData %3 == 0) iter1.deleteCurrent();// delete it } // end while theLists.displayList();// display the list } // end main(); } // end class InterIterApp Code shows how you could delete items with keys that are Multiples of 3.

47  We inserted five links and display the list  Then we iterate through he list deleting those links with keys divisible by three and display the list again.  Output is:  Should always check to see if list is empty first!

48 Additional References

49 Iterators and Doubly-Linked Lists - Links  ring2005/Old%20cs160/lecture10.html ring2005/Old%20cs160/lecture10.html

50 Iterator Methods  Can add additional methods to the iterator objects to make the iterator match one’s needs.  Note: the use of the iterator will give us access to desired Links. We may need common iterator methods as:  reset() – sets the iterator to the start of the list  nextLink() – Moves the iterator to the next link  getCurrent() – returns the link at the iterator  atEnd() – returns true if the iterator is at the EOL  insertAfter() – inserts a new link after the iterator  insertBefore() – inserts a new link before the iterator  deleteCurrent() – deletes the link at the iterator.  Must decide what tasks are performed by an iterator and which by the list itself.

51 Important Concept  Which methods to associate with the list and which methods are better associated with the iterator?  Book points out that an insertBefore() method may be best found in an iterator, because the iterator knows where it is…  insertFirst() might be best found in the list class, which ‘knows’ the start of the list.  Many methods can go either place and this depends on your processing needs.

52 The interIterator.java Program  The interIterator.java program includes an interface that allows the user to control the iterator directly.  Once the program is started, you can perform the following operations by providing the appropriate inputs:  S – show the list contents  R – reset the iterator to the start of the list  N – go to the next link  G – get the contents of the current link  B – insert before the current link  A – Insert a new link after the current link  D – delete the current link  Let’s examine the code: