CHAPTER 19 - PowerPoint PPT Presentation

chapter 19 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CHAPTER 19 PowerPoint Presentation
play fullscreen
1 / 78
CHAPTER 19
205 Views
Download Presentation
yama
Download Presentation

CHAPTER 19

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. CHAPTER 19 An Introduction to Data Structures

  2. CHAPTER GOALS • To learn how to use linked lists provided in the standard library • To be able to use iterators to transverse linked lists • To understand the implementation of linked lists • To distinguish between abstract and concrete data types • To know the efficiency of fundamental operations of lists and arrays • To become familiar with the stack and queue types

  3. LinkedList • 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 a linked list is efficient. • Visiting the elements of a linked list in sequential order is efficient • Random access is not efficient

  4. Insertingan Element into a Linked List

  5. Java's LinkedList class • Easy access to first and last elements with methods • void addFirst(Object obj) • void addLast(Object obj) • Object getFirst() • Object getSecond() • Object removeFirst() • Object removeLast()

  6. ListIterator • List iterator gives access to elements inside a linked list • ListIterator protects the linked list while giving access • ListIterator encapsulates a position anywhere in the linked list

  7. A List Iterator

  8. Conceptual View of the ListIterator

  9. List Iterator • Think of an iterator as pointing between two links

  10. List Iterator • The listIterator method of the LinkedList class gets a list iterator LinkedList list = . . . ListIterator iterator = list.listIterator();

  11. List Iterator • Thenextmethodmoves the iteratoriterator.next(); • nextthrowsa NoSuchElementException if you are already past the end of the list

  12. List Iterator • hasNextreturns true if there is a next element if (iterator.hasNext()) iterator.next();

  13. List Iterator • The next method returns the object of the link that it is passing while iterator.hasNext() { Object obj = iterator.next(); //do something with the object }

  14. List Iterator • To move the list position backwards, use: • hasPrevious • previous

  15. Adding and Removing from a LinkedList • The add method: • Adds an object after the iterator • Moves the iterator position past the new element iterator.add("Juliet");

  16. Adding and Removing from a LinkedList • The remove method: • Removes and • Returns the object that was returned by the last call to next or previous

  17. Adding and Removing from a LinkedList • This loop removes all objects that fulfill a certain condition while (iterator.hasNext()) { Object obj = iterator.next(); if (obj fulfills condition) iterator.remove(); }

  18. File ListTest.java • ListTest is a sample program that • inserts elements into a list • iterates through the list, adding and removing elements • prints the list

  19. File ListTest.java 01: import java.util.LinkedList; 02: import java.util.ListIterator; 03: 04: /** 05: A program that demonstrates the LinkedList class 06: */ 07: public class ListTest 08: { 09: public static void main(String[] args) 10: { 11: LinkedList staff = new LinkedList(); 12: staff.addLast("Dick"); 13: staff.addLast("Harry"); 14: staff.addLast("Romeo"); 15: staff.addLast("Tom"); 16: 17: // | in the comments indicates the iterator position

  20. 18: 19: ListIterator iterator = staff.listIterator(); // |DHRT 20: iterator.next(); // D|HRT 21: iterator.next(); // DH|RT 22: 23: // add more elements after second element 24: 25: iterator.add("Juliet"); // DHJ|RT 26: iterator.add("Nina"); // DHJN|RT 27: 28: iterator.next(); // DHJNR|T 29: 30: // remove last traversed element 31: 32: iterator.remove(); // DHJN|T 33: 34: // print all elements 35: 36: iterator = staff.listIterator(); 37: while (iterator.hasNext())

  21. 38: System.out.println(iterator.next()); 39: } 40: }

  22. Implementing Linked Lists • LinkedListclass has a private inner class Link class LinkedList { private class Link { public Object data; public Link next; } }

  23. Implementing Linked Lists • LinkedList class • Holds a reference first to the first link • Has a method to get the first element

  24. Implementing Linked Lists class LinkedList { public LinkedList() { first = null; } public Object getFirst() { if (first == null) throw new NoSuchElementException(); return first.data; } . . . private Link first; }

  25. Adding a New First Element • When a new link is added to the list • It becomes the head of the list • The old first link becomes the next link

  26. Adding a New First Element • The addFirst method class LinkedList { . . . public void addFirst(Object obj) { Link newLink = new Link(); newLink.data = obj; newLink.next = first; first = newLink; } ... }

  27. Adding a Link to the Head of a Linked List

  28. Removing the First Element • When the first element is removed • The data of the first link are saved and later returned as the method result • The successor of the first link becomes the first link of the shorter list • The link will be garbage collected when there are no further references to it

  29. Removing the First Element • TheremoveFirstmethod class LinkedList { . . . public Object removeFirst() { if (first == null) throw new NoSuchElementException(); Object obj = first.data; first = first.next; return obj; } . . . }

  30. Removing the First Link from a Linked List

  31. LinkedListIterator • Private inner class of LinkedList • Implements a simplified ListIterator interface • Has access to the first field and private Link class

  32. LinkedListIterator The LinkListIterator class class LinkedList { . . . public ListIterator listIterator() { return new LinkedListIterator(); } private class LinkedListIterator implements ListIterator { public LinkedListIterator() { position = null; previous = null; } . . . private Link position; private Link previous; } . . . }

  33. LinkListIterator's next Method • positionreference is advances toposition.next • Old position is remembered asprevious • If the iterator points before the first element of the list, then the oldpositionisnullandpositionmust be set tofirst

  34. LinkListIterator's next Method private class LinkedListIterator implements ListIterator { . . . public Object next() { if (!hasNext()) throw new NoSuchElementException(); previous = position; // remember for remove if (position == null) position = first; else position = position.next; return position.data; } . . . }

  35. LinkListIterator's hasnext Method • 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)

  36. LinkListIterator's hasnext Method private class LinkedListIterator implements ListIterator { . . . public boolean hasNext() { if (position == null) return first != null; else return position.next != null; } . . . }

  37. LinkListIterator's remove Method • If the element to be removed is the first element, call removeFirst • Otherwise, the link proceeding the element to be removed needs to have its next reference updated to skip the removed element • If the previous reference is null: • this call does not immediately follow a call to next • throw an IllegalArgumentException • Set previous reference to null

  38. LinkListIterator's remove Method private class LinkedListIterator implements ListIterator { . . . public void remove() { if (position == first) { removeFirst(); position = first; } else { if (previous == null) throw new IllegalStateException(); previous.next = position.next; position = previous; } previous = null; } . . . }

  39. Removing a Link From the Middle of a Linked List

  40. LinkListIterator's set Method • Changes the data stored in the previously visited element • The set method private class LinkedListIterator implements ListIterator { . . . public void set(Object obj) { if (position == null) throw new NoSuchElementException(); position.data = obj; } . . . }

  41. LinkListIterator's add Method • Inserts the new link after the current position • Sets the successor of the new link to the successor of the current position

  42. LinkListIterator's add Method private class LinkedListIterator implements ListIterator { . . . public void add(Object obj) { if (position == null) { addFirst(obj); position = first; } else { Link newLink = new Link(); newLink.data = obj; newLink.next = position.next; position.next = newLink; position = newLink; } previous = null; } . . . }

  43. Adding a Link to the Middle of a Linked List

  44. File LinkedList.java 001: import java.util.NoSuchElementException; 002: 003: /** 004: A linked list is a sequence of links with efficient 005: element insertion and removal. This class 006: contains a subset of the methods of the standard 007: java.util.LinkedList class. 008: */ 009: public class LinkedList 010: { 011: /** 012: Constructs an empty linked list. 013: */ 014: public LinkedList() 015: { 016: first = null; 017: }

  45. 018: 019: /** 020: Returns the first element in the linked list. 021: @return the first element in the linked list 022: */ 023: public Object getFirst() 024: { 025: if (first == null) 026: throw new NoSuchElementException(); 027: return first.data; 028: } 029: 030: /** 031: Removes the first element in the linked list. 032: @return the removed element 033: */ 034: public Object removeFirst() 035: { 036: if (first == null) 037: throw new NoSuchElementException();

  46. 038: Object obj = first.data; 039: first = first.next; 040: return obj; 041: } 042: 043: /** 044: Adds an element to the front of the linked list. 045: @param obj the object to add 046: */ 047: public void addFirst(Object obj) 048: { 049: Link newLink = new Link(); 050: newLink.data = obj; 051: newLink.next = first; 052: first = newLink; 053: } 054: 055: /** 056: Returns an iterator for iterating through this list. 057: @return an iterator for iterating through this list

  47. 058: */ 059: public ListIterator listIterator() 060: { 061: return new LinkedListIterator(); 062: } 063: 064: private Link first; 065: 066: private class Link 067: { 068: public Object data; 069: public Link next; 070: } 071: 072: private class LinkedListIterator implements ListIterator 073: { 074: /** 075: Constructs an iterator that points to the front 076: of the linked list. 077: */

  48. 078: public LinkedListIterator() 079: { 080: position = null; 081: previous = null; 082: } 083: 084: /** 085: Moves the iterator past the next element. 086: @return the traversed element 087: */ 088: public Object next() 089: { 090: if (!hasNext()) 091: throw new NoSuchElementException(); 092: previous = position; // remember for remove 093: 094: if (position == null) 095: position = first; 096: else 097: position = position.next;

  49. 098: 099: return position.data; 100: } 101: 102: /** 103: Tests if there is an element after the iterator 104: position. 105: @return true if there is an element after the iterator 106: position 107: */ 108: public boolean hasNext() 109: { 110: if (position == null) 111: return first != null; 112: else 113: return position.next != null; 114: } 115: 116: /** 117: Adds an element before the iterator position

  50. 118: and moves the iterator past the inserted element. 119: @param obj the object to add 120: */ 121: public void add(Object obj) 122: { 123: if (position == null) 124: { 125: addFirst(obj); 126: position = first; 127: } 128: else 129: { 130: Link newLink = new Link(); 131: newLink.data = obj; 132: newLink.next = position.next; 133: position.next = newLink; 134: position = newLink; 135: } 136: previous = null; 137: }