/* Linked List Object
 *
 *      Creates an unordered linked list with a head and tail
 *      Delete method is not implemented as this list only grows
 */
function LinkedList ()
{
        // Define a queue object

        // define attributes
        this.head
        this.tail

        // define responsibilities
        this.insert = _insert;
        this.search = _searchList;
        this.toString = _printList;
        this.clear = _clearList;
	this.loadOption = _loadOption;
}

/* insert method
 *
 * parameters:
 *      k - object to be inserted into the linked list
 */
function _insert (k)
{
        if (this.head){
                // The head exists, so keep adding new ones to the tail
                // The size is virtually unlimited as we just allocate
                // new nodes as we need them
                this.tail.next = new LinkedListItem (k);
                this.tail = this.tail.next
        }else{
                // There is no head, so make a new one and set up the tail
                this.head = new LinkedListItem (k);
                this.tail = this.head
        }
}

/* _searchList method
 *
 * parameters:
 *      k - object to be searched in the linked list
 *              (requires a label accessor to search on)
 */
function _searchList (k)
{
        var x = this.head;

        while (x && x.key.getId() != k) x = x.next;

        if (x)
                return x.key;
        else
                return null;
}

/* _printList method
 *
 * returns:
 *      the contents of the list by calling the print method
 *      of each enclosed object
 */
function _printList ()
{
        var retList = "";
        var x = this.head;
        while (x)
        {
                retList += x.key.toString() +"\n";
                x = x.next;
        }
        return retList;
}

/* _clearList method
 *
 * resets the head and tail pointers to null
 */
function _clearList ()
{
        this.head = null;
        this.tail = null;
}

/* Load Option from Linked List
 *
 * parameters:
 * 	optionbox - Drop-down option box object
 *	erase_flag - Erase contents first (true)
 */
function _loadOption (optionbox, erase_flag)
{
	if (erase_flag) {optionbox.length = 0;}
	
        var x = this.head;
        while (x)
        {
		optionbox.options[optionbox.length] = x.key.loadOption();
                x = x.next;
        }

}
/* Linked List Item Object
 *
 * parameters:
 *      key - object to be stored in the node
 */
function LinkedListItem (key)
{
        // define attributes
        this.key = key;
        this.next = null;
}

