Class SinglyLinkedList<E>

java.lang.Object
com.zybooks.dsaj.fundamental.SinglyLinkedList<E>
Type Parameters:
E - the element type stored at a position
All Implemented Interfaces:
Cloneable

public class SinglyLinkedList<E> extends Object implements Cloneable
A basic singly linked list implementation.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an initially empty list.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds an element to the front of the list.
    void
    Adds an element to the end of the list.
     
    boolean
     
    Returns (but does not remove) the first element of the list
    int
     
    boolean
    Tests whether the linked list is empty.
    Returns (but does not remove) the last element of the list.
    Removes and returns the first element of the list.
    int
    Returns the number of elements in the linked list.
    Produces a string representation of the contents of the list.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • SinglyLinkedList

      public SinglyLinkedList()
      Constructs an initially empty list.
  • Method Details

    • size

      public int size()
      Returns the number of elements in the linked list.
      Returns:
      number of elements in the linked list
    • isEmpty

      public boolean isEmpty()
      Tests whether the linked list is empty.
      Returns:
      true if the linked list is empty, false otherwise
    • first

      public E first()
      Returns (but does not remove) the first element of the list
      Returns:
      element at the front of the list (or null if empty)
    • last

      public E last()
      Returns (but does not remove) the last element of the list.
      Returns:
      element at the end of the list (or null if empty)
    • addFirst

      public void addFirst(E e)
      Adds an element to the front of the list.
      Parameters:
      e - the new element to add
    • addLast

      public void addLast(E e)
      Adds an element to the end of the list.
      Parameters:
      e - the new element to add
    • removeFirst

      public E removeFirst()
      Removes and returns the first element of the list.
      Returns:
      the removed element (or null if empty)
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • clone

      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Produces a string representation of the contents of the list. This exists for debugging purposes only.
      Overrides:
      toString in class Object