// Quickly banged together Linked-list // 4/17/2001 // Note that items are added 'before' the last one and that the toArray() // function reverses them in the returned array, so they appear in order. // No 'remove' function, just a cheap trick to lose/clear the list. Hope your // garbage collector is working well. // Distribute it however you like. public class MyLinkedList { MyLinkedListNode head; public MyLinkedList() { head = new MyLinkedListNode(); head.setNext(null); } public void add(Object data) { MyLinkedListNode temp = new MyLinkedListNode(data); temp.setNext(head); head = temp; } public int size() { int count = 0; MyLinkedListNode temp = head; while (temp.getNext() != null) { temp = temp.getNext(); count++; } return count; } public Object[] toArray() { Object tempArray[] = new Object[size()]; int index = size()-1; MyLinkedListNode temp = head; while (temp.getNext() != null) { tempArray[index] = temp.getData(); temp = temp.getNext(); index--; } return tempArray; } public void clear() { head = new MyLinkedListNode(); } }