





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!






public interface Enumeration { boolean hasMoreElements(); Object nextElement(); }
// This code will enumerate the elements of a vector v .. Enumeration e = v.elements(); while (e.hasMoreElements()) { System.out.println(e.nextElement()); }
Iterator i=v.iterator(); while (i.hasNext()) { i.next(); i.remove(); }
// For singly linked lists … public class LinkedList { public boolean isEmpty(); public int size(); public void addFIrst(Object item); public Object firstItem(); public void removeFirst(); public boolean contains(Object item); public void reverse(); public Iterator iterator(); public boolean equals(Object other); public String toString(); }
class ListNode { Object data; ListNode next; ListNode(Object data, ListNode next) { this.data = data; this.next = next; } } // this operator is self referential …
public void reverse() { ListNode current = head; head = null; while (current != null) { ListNode save = current; current = current.next; save.next = head; head = save; } }
public Enumeration elements(); // add to class def public Enumeration elements() { return new Enumeration() { // anonymous inner class private ListNode current = head; public boolean hasMoreElements() { return (current != null); } public Object nextElements() { if (current != null) { Object result = current.data; current = current.next; return results; } else { throw new NoSuchElemenExceprion(): } } }; }
public boolean equals(Object obj) { return (this == obj); }