/*
 * LinkedList.java
 * 
 * Walter Goodwater
 * 
 * CSc 300: Copyright Violations
 * 
 * This code modified from "Data Structures and Algorithm Analysis
 *  in Java", by Weiss
 */

public class LinkedList
{
	public ListNode head;
	
	public LinkedList( )
	{
		head = new ListNode( null, 0 );
	}
	
	public void insert( String name, int age )
	{
		/* if the list is empty */
		if( head == null )
		{
			head = new ListNode( name, age );
		}
		/* if the list is non-empty and the insert goes before the header */
		else if( head.age < age )
		{
			head = new ListNode( name, age, head );
		}
		/* if the insert goes in the middle */
		else
		{
			LinkedListItr iter = new LinkedListItr( head, head.next );
			while( iter.current != null && iter.current.age > age )
			{
				iter.advance();
			}
			/* if insert goes at the end of the list */
			if( iter.current == null )
			{
				iter.current = new ListNode( name, age );
			}
			/* if insert is between two nodes */
			else if( iter.current.age < age )
			{
				iter.previous.next = new ListNode( name, age );
				iter.previous.next.next = iter.current;
			}
		}
	}
	
	public void delete( String name )
	{
		/* if the list is empty */
		if( head == null )
		{
			System.out.println( "List empty -- nothing to remove" );
			System.exit(-1);
		}
		/* if the header is to be deleted */
		if( head.name.equals( name ) )
		{
			head = head.next;
		}
		else
		{
			LinkedListItr iter = new LinkedListItr( head, head.next );
			while( iter.current != null && !(iter.current.name.equals(name)) )
			{
				iter.advance();
			}
			/* if given name was not in list */
			if( iter.current == null )
			{
				System.out.println( "Name not found" );
				System.exit(-1);
			}
			if( iter.current.name.equals(name) )
			{
				iter.previous.next = iter.current.next;
			}
		}
	}
}