// generates a list of the elements in the collection // Postcondion: list is independant (deep copy) of original collection public List<T> createList() { List<T> list = new ArrayList<T>(); LLNode<T> temp = node; while (temp.getLink() != null) { list.add(temp.getInfo()); temp = temp.getLink(); } list.add(temp.getInfo()); return list; }
// Adds element to this collection. // Precondition: element is not already in the collection public void add(T element) { if (node == null) { node = new LLNode<T>(element); size++; } else { LLNode<T> temp = node; while (temp.getLink() != null) { temp = temp.getLink(); } temp.setLink(new LLNode<T>(element)); size++; } }
// Returns the element if this collection contains an element e such that // e.compareTo(element) == 0; otherwise, returns null. public T fetch(T element) { if (size == 0) { return null; } else { LLNode<T> temp = node; while (temp.getLink() != null) { if (element.compareTo(temp.getInfo()) == 0) { return temp.getInfo(); } temp = temp.getLink(); } if (temp.getInfo().compareTo(element) == 0) { return temp.getInfo(); } } return null; }