Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 24 ADT Part V (Linked List Using Iterator) Overview  Utility Classes.  List Iterator.  View of the List Iterator.  Adding to the Head of.

Similar presentations


Presentation on theme: "1 Lecture 24 ADT Part V (Linked List Using Iterator) Overview  Utility Classes.  List Iterator.  View of the List Iterator.  Adding to the Head of."— Presentation transcript:

1 1 Lecture 24 ADT Part V (Linked List Using Iterator) Overview  Utility Classes.  List Iterator.  View of the List Iterator.  Adding to the Head of a Linked List.  Removing to the Head of a Linked List.  Removing a Link from the Middle of a Linked List.  Adding a Link to the Middle of a Linked List.  Example 1.  Example 2.  Preview:

2 2 Lecture 24 Iterator Interface  An Iterator is an interface that can be implemented by a collection class. The Iterator interface in java.util declares three methods. An Iterator object allows you to access all the objects it contains serially– but only once. There’s no way to go back to the beginning!!! MethodDescription next() Returns an object as type Object starting with the first, and sets the Iterator object to return the next object on the next call of this method. If there is no object to be returned the method throws NoSuchElementException exception. hasNext() Returns true if there is a next object to be retrieved by a call to next(). remove() Removes the last object returned by next() from the collection that supplied the Iterator object. If next() has not been called or if you call remove() twice after calling next(), an IllegalStateException will be thrown.

3 3 Lecture 24 ListIterator Interface The ListIterator interface provides methods for traversing the objects in a collection backward or forward !!! MethodDescription next() Retrieves the next object in sequence- the same as for the Iterator interface. hasNext() Returns true if there is an object that will be returned by next(). previous() Returns the previous object in sequence in the list. You use this method to run backwards through the list. hasPrevious() Returns true if the next call to previous() will return an object. previousIndex() Returns the index of the object that will be returned by the next call to previous(), or returns –1 if the ListIterator object is at the beginning.

4 4 Lecture 24 ListIterator Interface (Cont’d) MethodDescription remove()Removes the last object that was retrieved by next() or previous(). The UnsupportedOperation exception is thrown if the remove operation is not supported for this collection. add(Object obj)Adds the argument immediately before the object that would be returned by the next call to next(), and before the object that would returned by the next call to previous(). set(Object obj)Replaces the last object retrieved by a call to next() or previous(). If neither next() nor previous() have been called, or add() or remove() have been called most recently, a hasPrevious()Returns true if the next call to previous() will return an object previousIndex()Returns the index of the object that will be returned by the next call to previous(), or returns With a ListIterator you can add and replace objects, as well as remove them from the collection.

5 5 Lecture 24 ListIterator Interface (Cont’d) The LinkedList class in the java.util package implements linked lists. This linked list remembers both the first and the last link in the list. You have easy access to both ends of the list with the: void addFirst(object obj), void addLast(Object obj), Object get First(), Object removeFirst(), Object removeLast() The Java library supplies a ListIerator type. A list iterator encapsulates a position anywhere inside the linked list, think of the iterator as pointing between two links, just as the cursor in a word processor points between two characters. LinkedList Link Ali Link Hamed Link Fahad Link Saad Link Jameel

6 6 Lecture 24 View of the ListIterator Interface Think of the Iterator as pointing between two links,just as the cursor in a word processor points between two characters, each link element as being like a letter in a word processor, and think of the Iterator as being like the blinking cursor between letters. A H FS A H F S A J F H S

7 7 Lecture 24 /*Here is a sample program that inserts elements into a list and then iterates through the list, adding and removing elements. Finally, the entire list is printed. The commet indicate the iterator position*/ import java.util.LinkedList; import java.util.ListIterator; public class ListTest1 { public static void main(String[] args) { LinkedList staff = new LinkedList(); staff.addLast(“Ali"); staff.addLast("Hamed"); staff.addLast(“Fahad"); staff.addLast(“Saad"); // | in the comments indicates the iterator position ListIterator iterator = staff.listIterator(); // |AHFS iterator.next(); // A|HFS iterator.next(); // AH|FS Programming Example 1

8 8 Lecture 24 // add more elements after second element iterator.add("Jameel"); // AHJ|FS iterator.add(“Rami"); // AHJR|FS iterator.next(); // AHJRF|S // remove last traversed element iterator.remove(); // AHJR|S // print all elements iterator = staff.listIterator(); while(iterator.hasNext()) System.out.println(iterator.next()); } // End of main() method } // End of ListTest1 class Programming Example 1 (Cont’d)

9 9 Lecture 24 Adding to the Head of a Linked List class LinkedList { public void addFirst(Object obj) { Link newLink=new Link(); newLink.data =obj; newLink.next=first; first=newLink; } // End of addFirst() method } // End of LinkedList class Adding a Link to the Head of a Linked List first Link Ali data next Link Rami data next

10 10 Lecture 24 Removing from the Head of a Linked List Removing a Link from the Head of a Linked List LinkedList first Link Ali data next Link Rami data next class LinkedList { public Object removeFirst() { if (first == null) throw new NoSuchElementException(); Object obj = first.data; first=first.next; return obj; } // End of removeFirst() method } // End of LinkedList class

11 11 Lecture 24 Removing a Link from the Middle of a Linked List public class Iterator { public void remove() { if(position == first) removFist(); else { if(previous ==null) throw new IllegalStateException(); previous.next = position.next; position = previous; } previous = null; } // End of remove() method } // End of class Iterator Note: If the previous reference is null, then this call to remove does not immediately follow a call to next, and we throw an Illegal StateException. Otherwise, we set the successor of the previous element to its successor, thereby eliminating it. LinkedList first Link Ali data next Link Rami data next ListIterator previous position Link Fahad data next

12 12 Lecture 24 Adding a Link to the Middle of a Linked List public class Iterator { public void add(Object obj) { if(position == null) addFirst(obj); else { Link newLink = new Link(); newLink.data = obj; newLink.next = position.next; position.next = newLink; position = newLink; previous=null; } } } Note: You insert the new link after the current position, and set the successor of the new link to the successor of the current position. LinkedList first Link Ramidata next ListIterator previous position Link Fahaddata next Link Khaleddata next newLink Link Alidata next

13 13 Lecture 24 import java.util.NoSuchElementException; public class ListTest2 { public static void main(String[] args) { LinkedList staff = new LinkedList(); staff.addFirst(“Thamer"); staff.addFirst(“Rached"); staff.addFirst("Hamed"); staff.addFirst("Dhafer"); // | in the comments indicates the iterator position LinkedList.Iterator iterator = staff.listIterator(); // |DHRT iterator.next(); // D|HRT iterator.next(); // DH|RT // add more elements after second element iterator.add("Jameel"); // DHJ|RT iterator.add("Nawaf"); // DHJN|RT iterator.next(); // DHJNR|T Programming Example 2 // remove last traversed element iterator.remove(); // DHJN|T // print all elements iterator = staff.listIterator(); while (iterator.hasNext()) System.out.println(iterator.next()); } // End of main() method } // End of ListTest2 class

14 14 Lecture 24 /* A linked list is a sequence of links with efficient element insertion and removal. */ class LinkedList { /** Constructs an empty linked list. */ public LinkedList() { first = null; } // End of constructor LinkedList() /** Returns the first element in the linked list. @return the first element in the linked list. */ public Object getFirst() { if (first == null) throw new NoSuchElementException(); return first.data; } // End of getFirst() method /** Removes the first element in the linked list. @return the removed element. */ public Object removeFirst() { if (first == null) throw new NoSuchElementException(); Object obj = first.data; first = first.next; return obj; } // End of removeFirst() method Programming Example 2 (Cont’d)

15 15 Lecture 24 /** Adds an element to the front of the linked list. @param obj the object to add. */ public void addFirst(Object obj) { Link newLink = new Link(); newLink.data = obj; newLink.next = first; first = newLink; } // End of addFirst() method /** Returns an iterator for iterating through this list. @return an iterator for iterating through this list. */ public Iterator listIterator() { return new Iterator(); } // End of listIterator() method private Link first; private class Link { public Object data; public Link next; } Programming Example 2 (Cont’d)

16 16 Lecture 24 public class Iterator { /** Constructs an iterator that points to the front of the linked list. */ public Iterator() { position = null; previous = null; } /** Moves the iterator past the next element. @return the traversed element. */ public Object next() { if (position == null) { position = first; return getFirst(); } else { if (position.next == null) throw new NoSuchElementException(); previous = position; // remember for remove position = position.next; return position.data; } } // End of next() method Programming Example 2 (Cont’d)

17 17 Lecture 24 /** Tests whether there is an element after the iterator position. @return true if there is an element after the iterator position. */ public boolean hasNext() { if (position == null) return first != null; else return position.next != null; } /** Adds an element before the iterator position and moves the iterator past the inserted element. @param obj the object to add. */ public void add(Object obj) { if (position == null) addFirst(obj); else { Link newLink = new Link(); newLink.data = obj; newLink.next = position.next; position.next = newLink; position = newLink; previous = null; } } // End of add() method Programming Example 2 (Cont’d)

18 18 Lecture 24 /** Removes the last traversed element. This method may be called only after a call to the next() method. */ public void remove() { if (position == first) removeFirst(); else { if (previous == null) throw new IllegalStateException(); previous.next = position.next; position = previous; } previous = null; } // End of remove() method. private Link position; private Link previous; } // End of inner class Iterator } // End of class LinkedList Programming Example 2 (Cont’d)


Download ppt "1 Lecture 24 ADT Part V (Linked List Using Iterator) Overview  Utility Classes.  List Iterator.  View of the List Iterator.  Adding to the Head of."

Similar presentations


Ads by Google