previous | start | next

Remove Front

Removing the first value and assigning it to a variable x is easy, but we have to be careful if the list is empty or if it has only one data value:

public E removeFront()
{
  E x;
  if ( start == null ) {        // case 1: empty list
    throw new NoSuchElementException();
  } else if ( start == last ) { // case 2: only 1 element
    x = start.data;
    start = last = null;     
  } else {                      // case 3: more than 1 element   
    x = start.data;
    start = start.next;
  }
  return x;
}
   

(Draw a before and after picture for each of the cases!)



previous | start | next