

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


CMCS 433, Fall 2001 - Alan Sussman 2
CMCS 433, Fall 2001 - Alan Sussman 3
CMCS 433, Fall 2001 - Alan Sussman 5
public class LinkedList { // Keep this private; no one else see the implementation private static class Node { Object value; Node next; Node(Object v) { value = v; next = null; } }; // Put here to show that this is the Transformer for LinkedList public static interface Transformer {public Object transform(Object v); } Node head, tail; public void applyTransformer(Transformer t) { for (Node n = head; n != null; n = n.next) n.value = t.transform(n.value); } public void append(Object v) { Node n = new Node(v); if (tail == null) head = n; else tail.next = n; tail = n; } }
public class getStringRep implements LinkedList.Transformer { public Object transform(Object o) { return o.toString(); }
CMCS 433, Fall 2001 - Alan Sussman 7
CMCS 433, Fall 2001 - Alan Sussman 8
public class FixedStack { Object [] array; int top = 0; class MyEnum implements java.util.Enumerator { int count = top; public boolean hasMoreElements() { return count > 0; } public Object nextElement() { if (count == 0) throw new NoSuchElementException(“FixedStack”); return array[--count]; } } public java.util.Enumerator enumerateAll() { return new MyEnum(); } }
CMCS 433, Fall 2001 - Alan Sussman 9
CMCS 433, Fall 2001 - Alan Sussman 10
public class FixedStack { Object [] array; int top = 0; public java.util.Enumerator enumerateOldestK(final int k) { class MyEnum implements java.util.Enumerator { int pos = 0; public boolean hasMoreElements() { return pos < k && pos < top; } public Object nextElement() { if (!hasMoreElements()) { throw new NoSuchElementException(“FixedStack”); return array[pos++]; } } return new MyEnum(); } }
CMCS 433, Fall 2001 - Alan Sussman 11
public class FixedStack { Object [] array; int top = 0; public java.util.Enumerator enumerateOldestK(final int k) { return new java.util.Enumerator() { int pos = 0; public boolean hasMoreElements() { return pos < k && pos < top; } public Object nextElement() { if (!hasMoreElements()) throw new NoSuchElementException(“FixedStack”); return array[pos++]; } } } } (^) CMCS 433, Fall 2001 - Alan Sussman 12