//---------------------------------------------------------------------- // LinkedStringLog.java by Dale/Joyce/Weems Chapter 2 // // Implements StringLogInterface using a linked list // of LLStringNode to hold the log strings. // ajg version: formatting; private; while->for; if //---------------------------------------------------------------------- package ch02.stringLogs; public class LinkedStringLog implements StringLogInterface { private String name; // name of this StringLog private LLStringNode log = null ; // reference to the first node of // linked list holding the strings // Create an empty StringLog object with name "name". public LinkedStringLog(String name) { this.name = name; } public void insert(String element) { // Precondition: This StringLog is not full. LLStringNode newNode = new LLStringNode(element); newNode.setLink(log); log = newNode; } public boolean isFull() { return false; } // Return the number of Strings in this StringLog. public int size() { int count = 0; for (LLStringNode node=log; node!=null; node=node.getLink()) count++; return count; } public boolean contains(String element) { // Ignore case difference when doing string comparison. for (LLStringNode node=log; node!=null; node=node.getLink()) if (element.equalsIgnoreCase(node.getInfo())) // they match return true; return false; } public void clear() { log = null; } public String getName() { return name; } // Return a nicely formatted string representing this StringLog. public String toString() { String ans = "Log: " + name + "\n\n"; int count = 0; for (LLStringNode node=log; node!=null; node=node.getLink()) ans += (++count) + ". " + node.getInfo() + "\n"; return ans; } } // Local Variables: // compile-command: "cd ../..; javac ch02/stringLogs/LinkedStringLog.java" // End: