// UnsortedList.java // implementation // public class UnsortedList { private int length; private ListNode listData; private ListNode currentPos; public UnsortedList() { length = 0; listData = null; currentPos = null; } public boolean isEmpty() { return length==0; } public void insert(String item) { length ++; ListNode temp = new ListNode(item, null); if(isEmpty()) { listData = temp; currentPos = temp; } else { temp.setLink(listData); listData = temp; currentPos = temp; } } public void printFromCurrent() { ListNode temp = currentPos; System.out.println("The list is: "); while(temp != null) { System.out.print(temp.getData() + " "); temp = temp.getLink(); } System.out.println(); } public void reset() { currentPos = listData; } public String getCurentItem() { return currentPos.getData(); } public String getNextItem() { ListNode temp = currentPos; temp = temp.getLink(); return temp.getData(); } public boolean search(String item) { boolean find = false; ListNode temp = listData; while(temp != null) { if(temp.getData().equalsIgnoreCase(item)) { find = true; currentPos = temp; } // move one node forward temp = temp.getLink(); } return find; } /* public void deleteItem(String item); */ public static void main(String[] args) { UnsortedList usl = new UnsortedList(); usl.insert("Jon"); usl.printFromCurrent(); usl.insert("Lee"); usl.printFromCurrent(); usl.insert("Tom"); usl.printFromCurrent(); if(usl.search("Tom")) System.out.println("We got it."); else System.out.println("The code has error."); } }