CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data...
Transcript of CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data...
![Page 1: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/1.jpg)
CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists
![Page 2: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/2.jpg)
Assignment 4: Due Sunday Oct 2 @ 10pm
Remember: javac –Xlint:all & checkstyle *.java & JUnit Solutions should be independently written!
![Page 3: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/3.jpg)
Part 1: Inheritence
![Page 4: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/4.jpg)
Why Inheritance?
Code Reuse • Subclass gets to use all methods of
the parent class “for free”
Overriding • Subclass can have more specific
implementation than the parent class
Design constraints • Subclasses get all of the features of
the parent, whether you like them or not!
Saying B inherits from A is a very
strong relationship: anytime that A could be used, B
could be instead
![Page 5: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/5.jpg)
Inheritance Types
B isa A
Square isa Rectangle
Single Inheritance
C isa B, B isa A
Square isa Rectangle Rectangle isa Shape
=> Square isa Shape
Multilevel Inheritance
C isa A, C isa B
Mike isa CS Prof Mike isa Bio Prof
Multiple Inheritance
![Page 6: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/6.jpg)
Inheritance versus Encapsulation
B isa A
Square isa Rectangle
Pokemon Trainer
Position pos String name String team List<Pokemon> List<Eggs> Backpack pack
Position
Double lat Double long Double altitude
Backpack
Int capacity List<Items> items
Trainer hasa Position, Trainer hasa Backpack,
etc
Encapsulation is used to hide the values or state of a structured data object inside a class,
![Page 7: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/7.jpg)
Part 2: Lists
![Page 8: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/8.jpg)
List Queue insertFront insertBack
removeBack removeFront
addme.next = first; first = addme; last.next = addme; addme.next = null
first = first.next; ???
![Page 9: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/9.jpg)
removeLast (Singly Linked List)
Queue
first
last
Node
4
next
Node
3
next
Node
2
next
Node
1
next null
Oops, just made remove an O(n) operation How might you address this?
front ???
![Page 10: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/10.jpg)
Deque
first
last
null
Node
1 next prev
Deque with Doubly Linked List
Node
2 next prev
Node
3 next prev
Node
4 next prev
null
Very similar to a singly linked list, except each node has a reference to both the next and previous node in the list
A little more overhead, but significantly increased flexibility: supports insertFront, insertBack, removeFront, removeBack,
insertBefore, removeMiddle
![Page 11: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/11.jpg)
Dequeue Interface public interface Dequeue<T> {
boolean empty();int length();
T front();T back();
void insertFront(T t);void insertBack(T t);
void removeFront();void removeBack();
}
How would you implement a deque with a doubly linked list?
How would you implement a general List<T> class?
public class MyDeque implements Dequeue<T> {
private static class Node<T>{T dataNode<T> next;Node<T> prev;
}
private Node<T> front;private Node<T> back;
T front() { return front.data; }T back() { return back.data; }
...}
![Page 12: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/12.jpg)
List v1 public interface Node<T> {
void setValue(T t); T getValue();
void setNext(Node<T> n);void setPrev(Node<T> n);
void getNext(Node<T> n);void getPrev(Node<T> n);
}
public interface List<T> {boolean empty();int length();
Node<T> front() throws EmptyException;Node<T> back() throws EmptyException;
void insertFront(Node<T> t);void insertBack(Node<T> t);
void insertBefore(Node<T> t);void insertAfter(Node<T> t);
void removeFront() throws EmptyException;void removeBack() throws EmptyException;void removeNode(Node<T> t);
}
Is this a good design?
No! We expose that Lists use Nodes for underlying storage. Worse, client programs may incorrectly edit the next/prev
references
![Page 13: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/13.jpg)
List v2 public class MyList<T> implements List<T> {
private static class Node<T> {T data;Node<T> next;Node<T> prev;
}
boolean empty() { ... }int length() { ... }
T front() throws EmptyException { ... }T back() throws EmptyException { ... }
void insertFront(T t) { ... }void insertBack(T t) { ... }
void insertBefore(???) { ... }void insertAfter(???) { ... }
void removeFront() throws EmptyException { ... }void removeBack() throws EmptyException { ... }void removeNode(???) { ... }
}
Is this a good design?
Better, the nested class encapsulates the storage medium, but restricts what methods can be implemented to very
first/last elements
Some implementations will have public node classes with private next/prev references. However they also have
public setNext(), setPrev() => poor encapsulation!
![Page 14: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/14.jpg)
List v3 public class MyList<T> implements List<T> {
private static class Node<T> {T data;Node<T> next;Node<T> prev;
}
boolean empty() { ... }int length() { ... }
T front() throws EmptyException { ... }T back() throws EmptyException { ... }void insertFront(T t) { ... }void insertBack(T t) { ... }
void insertBefore(int idx) { ... }void insertAfter(int idx) { ... }
void removeFront() throws EmptyException { ... }void removeBack() throws EmptyException { ... }void removeNode(int idx) { ... }
}
Is this a good design?
Slightly better: More flexible, but now insertBefore and removeNode() are
O(n) operations
![Page 15: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/15.jpg)
List v4 public interface Node<T> {
void setValue(T t); T getValue();
void setNext(Node<T> n);void setPrev(Node<T> n);
void getNext(Node<T> n);void getPrev(Node<T> n);
}
public interface List<T> {boolean empty();int length();
Node<T> front();Node<T> back();
void insertFront(Node<T> t);void insertBack(Node<T> t);
void removeFront();void removeBack();
}
![Page 16: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/16.jpg)
List v4 public interface Node<T> {
void setValue(T t); T getValue();
void setNext(Node<T> n);void setPrev(Node<T> n);
void getNext(Node<T> n);void getPrev(Node<T> n);
}
public interface List<T> {boolean empty();int length();
Node<T> front();Node<T> back();
void insertFront(Node<T> t);void insertBack(Node<T> t);
void removeFront();void removeBack();
}
public interface Position<T> { // empty on purpose}
public interface List<T> {// simplified interfaceint length();
Position<T> insertFront(T t);Position<T> insertBack(T t);void insertBefore(Position<T> t);void insertAfter(Position<T> t);
void removeAt(Position<T> p);}
![Page 17: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/17.jpg)
List v4 public interface Node<T> {
void setValue(T t); T getValue();
void setNext(Node<T> n);void setPrev(Node<T> n);
void getNext(Node<T> n);void getPrev(Node<T> n);
}
public interface List<T> {boolean empty();int length();
Node<T> front();Node<T> back();
void insertFront(Node<T> t);void insertBack(Node<T> t);
void removeFront();void removeBack();
}
“I am a position and while you can hold on to me, you can’t do anything else with me!”
Inserting at front or back creates the Position objects.
If you want, you could keep references to the Position
objects even in the middle of the list
Pass in a Position, and it will
remove it from the list
public interface Position<T> { // empty on purpose}
public interface List<T> {// simplified interfaceint length();
Position<T> insertFront(T t);Position<T> insertBack(T t);void insertBefore(Position<T> t);void insertAfter(Position<T> t);
void removeAt(Position<T> p);}
![Page 18: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/18.jpg)
List v4 public class NodeList<T> implements List<T> private static class Node<T> implements Position<T> { Node<T> next;
Node<T> prev;T data;List<T> color; // avoid “fake positions”
}
private Node<T> front; private Node<T> back; private int elements; public int length() { return this.elements; }
public Position<T> insertFront(T t) { ... }
public Position<T> insertBack(T t) { ...
}
public void remoteAt(Position<T> p) { ... }}
Public Position interface, but nested (private static) Node Implementation
![Page 19: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/19.jpg)
List v4 List l = new List<String>();
Position a = l.insertFront(“Mike”);Position b = l.insertBack(“Peter”);Position c = l.insertFront(“Kelly”);
l.removeAt(a);
public interface Position<T> { // empty on purpose}
public interface List<T> {// simplified interfaceint length();
Position<T> insertFront(T t);Position<T> insertBack(T t);
// TODO: void is temporaryvoid removeAt(Position<T> p);
}
List
front
back
null
Node
Kelly next prev
Node
Mike next prev
Node
Peter next prev
null
a b
c
![Page 20: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/20.jpg)
List v4 public String toString () { String s ="["; Node<T> n = this.front; while (n != null) { s += n.data.toString(); if (n.next != null) { s +=""; } n = n.next; } s +="]"; return s; }
$ java NodeList[Mike][Mike, Peter][Kelly, Mike, Peter][Kelly, Peter]
public static void main(String[] args) { List l = new NodeList(); Position a = l.insertFront("Mike"); System.out.println(l); Position b = l.insertBack("Peter"); System.out.println(l); Position c = l.insertFront("Kelly"); System.out.println(l);
l.remove(a); System.out.println(l);}
![Page 21: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/21.jpg)
How to test the code? public class NodeList<T> implements List<T> {
private static final class Node<T> implements Position<T> {Node<T> next;
Node<T> prev;T data;List<T> owner;public T get() { return this.data; }public void put(T t) { this.data = t; }
}...public Position<T> front() throws EmptyException { … }public Position<T> back() throws EmptyException { … }public Position<T> insertFront(T t) { … }public Position<T> insertBack(T t) { … }public void removeFront() throws EmptyException { … }public void removeBack() throws EmptyException { … }public Position<T> insertBefore(Position<T> p, T t)
throws PositionException { … }public Position<T> insertAfter(Position<T> p, T t)
throws PositionException { … }public void remove(Position<T> p) throws PositionException { … }public String toString() { … }
}
![Page 22: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/22.jpg)
How to test the code? public class NodeList<T> implements List<T> {
private static final class Node<T> implements Position<T> {Node<T> next;
Node<T> prev;T data;List<T> owner;public T get() { return this.data; }public void put(T t) { this.data = t; }
}...public Position<T> front() throws EmptyException { … }public Position<T> back() throws EmptyException { … }public Position<T> insertFront(T t) { … }public Position<T> insertBack(T t) { … }public void removeFront() throws EmptyException { … }public void removeBack() throws EmptyException { … }public Position<T> insertBefore(Position<T> p, T t)
throws PositionException { … }public Position<T> insertAfter(Position<T> p, T t)
throws PositionException { … }public void remove(Position<T> p) throws PositionException { … }public String toString() { … }
}
The output for every method should be tested, including any exceptions for invalid inputs
Use toString() method to test the state of private member
variables
![Page 23: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/23.jpg)
TestList.java (5) @Test(expected=InvalidPositionException.class) public void testRemoveAtFakePosition () {
List <String> faker = new NodeList<String>();Position <String> fake = faker.insertFront("Hehehe");list.insertBack("Paul");list.insertBack ("Mary");assertEquals("[Paul, Mary]", list.toString ()); assertEquals(2, list.length());list.removeAt(fake);
}}
Testing code is significantly longer than the implementation
J
![Page 24: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/24.jpg)
Part 3: Make Lists Useful Again J
![Page 25: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/25.jpg)
List v4 List l = new List<String>();
Position a = l.insertFront(“Mike”);Position b = l.insertBack(“Peter”);Position c = l.insertFront(“Kelly”);
l.removeAt(a);
public interface Position<T> { // empty on purpose}
public interface List<T> {// simplified interfaceint length();
Position<T> insertFront(T t);Position<T> insertBack(T t);
// TODO: void is temporaryvoid removeAt(Position<T> p);
}
List
front
back
null
Node
Kelly next prev
Node
Mike next prev
Node
Peter next prev
null
a b
c
![Page 26: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/26.jpg)
List v4 List l = new List<String>();
Position a = l.insertFront(“Mike”);Position b = l.insertBack(“Peter”);Position c = l.insertFront(“Kelly”);
l.removeAt(a);
public interface Position<T> { // empty on purpose}
public interface List<T> {// simplified interfaceint length();
Position<T> insertFront(T t);Position<T> insertBack(T t);
// TODO: void is temporaryvoid removeAt(Position<T> p);
}
List
front
back
null
Node
Kelly next prev
Node
Mike next prev
Node
Peter next prev
null
a b
c
This interface protects the integrity of the List, but is “weird” in that we can have references to objects but cant get their value or do anything else with them except remove them
![Page 27: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/27.jpg)
List v5 public interface Position<T> {
T get();void put(T t);
}
public interface List<T> {private static class Node<T> implements Position<T> {
Node<T> next; Node<T> prev; T data; List<T> color; public T get() { return this.data; }
public void put(T t) { this.data = t; }}
...}
![Page 28: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/28.jpg)
List v5 public interface Position<T> {
T get();void put(T t);
}
public interface List<T> {private static class Node<T> implements Position<T> {
Node<T> next; Node<T> prev; T data; List<T> color; public T get() { return this.data; }
public void put(T t) { this.data = t; }}
...}
Hooray, now we can get/set the value from a Position
Why wouldn’t you want to do it this way?
What if you wanted a UniqueList<T> that only stored unique items? This would have to be checked in the UniqueList<T>
implementation
![Page 29: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/29.jpg)
List v5 Test @Testpublic void testPositionGet() { Position<String> p1 = list.insertBack("Peter"); Position<String> p2 = list.insertBack("Paul"); assertEquals("[Peter Paul]", list.toString()); assertEquals("Peter", p1.get()); assertEquals("Paul", p2.get());}
@Testpublic void testPositionPut() { Position<String> p1 = list.insertBack("Peter"); list.insertBack("Paul"); assertEquals("[Peter Paul]", list.toString()); p1.put("Mary"); assertEquals("Mary", p1.get()); assertEquals("[Mary Paul]", list.toString());}
What else are we missing?
![Page 30: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/30.jpg)
List v6 public interface List<T> { ... Position<T> front() throws EmptyListException;
Position<T> back() throws EmptyListException;
Position<T> next(Position<T> p) throws InvalidPositionException;
Position<T> previous(Position<T> p) throws InvalidPositionException;
boolean hasNext(Position<T> p) throws InvalidPositionException;
boolean hasPrevious(Position<T> p) throws InvalidPositionException;
boolean valid(Position<T> p); …}
Why do we put next() and prev() into the list and not Position?
For more complex data structures, like trees or graphs, next() and prev() will be more complicated
![Page 31: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/31.jpg)
List v6 Iterating // Very C++ likePosition<String> current = list.front();Position<String> last = list.back();while (current != last) { // do whatever we need to do at the current position current = list.next(current);}
// More Java-likePosition<String> current = list.front();for (;;) { // do whatever we need to do at the current position if (list.hasNext(current)) { current = list.next(current); } else {
break; }
}
// Very Java-likePosition<String> current = list.front();while (list.valid(current)) { // do whatever we need to do at the current position current = list.next(current);}
![Page 32: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/32.jpg)
Iterator Interface public interface Iterator<T> { boolean valid(); void next(); // next element, not necessarily “next” T get(); // get ok, but put may break invariants}
public interface List<T> { ... Iterator<T> forwardIterator(); Iterator<T> backwardIterator(); ...}
Iterator<String> i = list.forwardIterator();while (i.valid()) { String e = i.get(); // do whatever with the element e i.next();}
![Page 33: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/33.jpg)
Iterator Interface private static class NodeListIterator<T> implements Iterator<T> { private Node<T> current; private boolean forward;
NodeListIterator(Node<T> start, boolean forward) { this.current = start; this.forward = forward;
}
public boolean valid() { return this.current != null; }
public void next() { this.current = this.forward ? this.current.next
: this.current.prev; }
public T get() { return this.current.get();
} }
Ternary operator: If (this.forward) {
this.current = this.current.next; } else {
this.current = this.current.prev; }
![Page 34: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/34.jpg)
Iterator Interface public Iterator<T> forwardIterator() { return new NodeListIterator<T>(this.front, true);}
public Iterator<T> backwardIterator() { return new NodeListIterator<T>(this.back, false);}
@Testpublic void testForwardIterator() { list.insertBack("Peter"); list.insertBack("Paul"); list.insertBack("Mary"); String[] expected = {"Peter", "Paul", "Mary"}; int current = 0; Iterator<String> i = list.forwardIterator(); while (i.valid()) { String e = i.get(); assertEquals(expected[current], e); i.next(); current += 1;
}assertEquals(3, current);
}
![Page 35: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/35.jpg)
Iterator Interface public Iterator<T> forwardIterator() { return new NodeListIterator<T>(this.front, true);}
public Iterator<T> backwardIterator() { return new NodeListIterator<T>(this.back, false);}
@Testpublic void testBackwardIterator() { list.insertBack("Peter"); list.insertBack("Paul"); list.insertBack("Mary"); String[] expected = {"Mary", "Paul", "Peter"}; int current = 0; Iterator<String> i = list.backwardIterator(); while (i.valid()) { String e = i.get(); assertEquals(expected[current], e); i.next(); current += 1;
} assertEquals(3, current);}
![Page 36: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/36.jpg)
Java Iterators http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html
![Page 37: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/37.jpg)
Java Iterators http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html
Will become even more important for more complex data structures to simplify certain operations like printing every element in sorted order
![Page 38: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/38.jpg)
Java Iterable https://docs.oracle.com/javase/7/docs/api/java/lang/Iterable.html
![Page 39: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/39.jpg)
List v7 public interface List<T> extends Iterable<T> { ...
private static class NodeListIterator<T> implements Iterator<T> {
...}
Iterator<String> i = list.iterator();while (i.hasNext()) { String e = i.next(); // do something with element e}
for (String e: list) { // do something with element e}
![Page 40: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/40.jpg)
Living in a null world
List
front
back
null
List
front
back
Node
Mike next prev
null
null
List
front
back
null
Node
Mike next prev
Node
Peter next prev
null
![Page 41: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/41.jpg)
Living in a null world
List
front
back
null
List
front
back
Node
Mike next prev
null
null
List
front
back
null
Node
Mike next prev
Node
Peter next prev
null
public Position <T> insertBack(T t) { ...if (this.back != null) {
this.back.next = n; }if (this.front == null) {
this.front = n; }...
}
public Position <T> insertFront(T t ) {...if (this.front != null) {
this.front.prev =n; }if (this.back==null) {
this.back = n; }...
}
public void removeAt(Position<T> p) {...if (n.next != null) { n.next.prev = n.prev; }if (n.prev != null) { n.prev.next = n.next; }...
}
![Page 42: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/42.jpg)
List
first
last
null
Node
f next prev
Doubly Linked List with Sentinels
Node
b next prev
null
![Page 43: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/43.jpg)
List
first
last
null
Node
f next prev
Doubly Linked List with Sentinels
Node
1 next prev
Node
b next prev
null
![Page 44: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/44.jpg)
List
first
last
null
Node
f next prev
Doubly Linked List with Sentinels
Node
1 next prev
Node
2 next prev
Node
b next prev
null
For the cost of a tiny bit of extra memory, the code gets significantly simpler!
![Page 45: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/45.jpg)
Part 4: Midterm Review!
![Page 46: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/46.jpg)
Next Steps 1. Reflect on the magic and power of stacks, queues, deques! 2. Work on Assignment 4: Due Sunday 3 Oct 2 @ 10:00 pm
3. Start to review for Midterm on Monday Oct 10
1. Your notes from class 2. Lecture Notes on Piazza 3. Slides on course webpage 4. Online & printed textbooks 5. Sample Midterm!!!
![Page 47: CS 600.226: Data Structuresphf/2016/fall/cs226/lectures/13.MoreLists.pdf · CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists](https://reader034.fdocuments.in/reader034/viewer/2022051916/60079e5169cafc37f36cad36/html5/thumbnails/47.jpg)
Welcome to CS 600.226 http://www.cs.jhu.edu/~cs226/
Questions?