// containers/Ex8.java // TIJ4 Chapter Containers, Exercise 8, page 821 /* Create a generic, singly linked list class called SList, which, to keep * things simple, does not implement the List interface. Each Link object in * the list should contain a reference to the next element in the list, but * not the previous one (LinkedList, in contrast, is a doubly linked list, * which means it maintains links in both directions). Create your own * SListIterator which, again for simplicity, does not implement ListIterator. * The only method in SList other than toString() should be iterator(), which * produces an SListIterator. The only way to insert and remove elements from * an SList is through SListIterator. Write code to demonstrate SList. */ import java.util.*; import static org.greggordon.tools.Print.*; class SList { private Link headLink = new Link(null); SListIterator iterator() { return new SListIterator(headLink); } public String toString() { if(headLink.next == null) return "SList: []"; print("SList: ["); SListIterator it = this.iterator(); StringBuilder s = new StringBuilder(); while(it.hasNext()) { s.append(it.next() + (it.hasNext() ? ", " : "")); } return s + "]"; } } class SListIterator { Link current; SListIterator(Link link) { current = link; } public boolean hasNext() { return current.next != null; } public Link next() { current = current.next; return current; } public void insert(E e) { current.next = new Link(e, current.next); current = current.next; } public void remove() { if(current.next != null) { current.next = current.next.next; } } } class Link { E e; Link next; Link(E e, Link next) { this.e = e; this.next = next; } Link(E e) { this(e, null); } public String toString() { if(e == null) return "null"; return e.toString(); } } public class Ex8 { public static void main(String[] args) { SList sl = new SList(); println(sl); SListIterator slIter = sl.iterator(); println("inserting \"hi\""); slIter.insert("hi"); println(sl); println("inserting \"there\""); slIter.insert("there"); println(sl); println("inserting \"sweetie\""); slIter.insert("sweetie"); println(sl); println("inserting \"pie\""); slIter.insert("pie"); println(sl); SListIterator slIter2 = sl.iterator(); println("removing \"hi\""); slIter2.remove(); println(sl); println("inserting \"hello\""); slIter2.insert("hello"); println(sl); println("removing \"there\""); slIter2.remove(); println(sl); println("removing \"sweetie\""); slIter2.remove(); println(sl); println("removing \"pie\""); slIter2.remove(); println(sl); println("removing \"hello\""); SListIterator slIter3 = sl.iterator(); slIter3.remove(); println(sl); } }