1 / 18

ADT Part V (Linked List Using Iterator)

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.

tavi
Télécharger la présentation

ADT Part V (Linked List Using Iterator)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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: Lecture 24

  2. 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!!! Lecture 24

  3. ListIterator Interface The ListIterator interface provides methods for traversing the objects in a collection backward or forward!!! Lecture 24

  4. ListIterator Interface (Cont’d) With a ListIterator you can add and replace objects, as well as remove them from the collection. Lecture 24

  5. ListIterator Interface (Cont’d) Link Link Link Link LinkedList Fahad Saad Hamed Ali Link Jameel 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. Lecture 24

  6. View of the ListIterator Interface A F S H F A H S J H F S A 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. Lecture 24

  7. Programming Example 1 /*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 Lecture 24

  8. Programming Example 1 (Cont’d) // 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 Lecture 24

  9. Adding to the Head of a Linked List LinkedList first Link Ali data next Adding a Link to the Head of a Linked List Link Rami data next 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 Lecture 24

  10. Removing from the Head of a Linked List LinkedList Link first Ali data next Removing a Link from the Head of a Linked List 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 Lecture 24

  11. Removing a Link from the Middle of a Linked List Link Link LinkedList Fahad Rami data data next next first Link ListIterator Ali data previous next position 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. Lecture 24

  12. Adding a Link to the Middle of a Linked List Link Link Link LinkedList data data data Khaled Fahad Rami next next next first ListIterator previous Link position data Ali next newLink 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. Lecture 24

  13. Programming Example 2 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 // 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 Lecture 24

  14. Programming Example 2 (Cont’d) /* 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 Lecture 24

  15. Programming Example 2 (Cont’d) /** 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; } Lecture 24

  16. Programming Example 2 (Cont’d) 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 Lecture 24

  17. Programming Example 2 (Cont’d) /** 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 Lecture 24

  18. Programming Example 2 (Cont’d) /** 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 Lecture 24

More Related