Retrieving the first item stored in the simple linked list

  • The getFirst( ) method retrieves the item stored in the first list element (= node) in the linked list:

  • The Implementation of the getFirst() method is as follows:

       // Returns the first element (item) in the list
       // returns generic type
       public T getFirst()
       {
          if(isEmpty())
          {
             throw new NoSuchElementException();
          }
          return first.item;  // Retrieves item in first node
       }