Class LinkedPositionalList<E>

java.lang.Object
com.zybooks.dsaj.list.LinkedPositionalList<E>
Type Parameters:
E - the element type
All Implemented Interfaces:
PositionalList<E>, Iterable<E>

public class LinkedPositionalList<E> extends Object implements PositionalList<E>
Implementation of a positional list stored as a doubly linked list.
  • Constructor Details

    • LinkedPositionalList

      public LinkedPositionalList()
      Constructs a new empty list.
  • Method Details

    • size

      public int size()
      Returns the number of elements in the list.
      Specified by:
      size in interface PositionalList<E>
      Returns:
      number of elements in the list
    • isEmpty

      public boolean isEmpty()
      Tests whether the list is empty.
      Specified by:
      isEmpty in interface PositionalList<E>
      Returns:
      true if the list is empty, false otherwise
    • first

      public Position<E> first()
      Returns the first Position in the list.
      Specified by:
      first in interface PositionalList<E>
      Returns:
      the first Position in the list (or null, if empty)
    • last

      public Position<E> last()
      Returns the last Position in the list.
      Specified by:
      last in interface PositionalList<E>
      Returns:
      the last Position in the list (or null, if empty)
    • before

      public Position<E> before(Position<E> p) throws IllegalArgumentException
      Returns the Position immediately before Position p.
      Specified by:
      before in interface PositionalList<E>
      Parameters:
      p - a Position of the list
      Returns:
      the Position of the preceding element (or null, if p is first)
      Throws:
      IllegalArgumentException - if p is not a valid position for this list
    • after

      public Position<E> after(Position<E> p) throws IllegalArgumentException
      Returns the Position immediately after Position p.
      Specified by:
      after in interface PositionalList<E>
      Parameters:
      p - a Position of the list
      Returns:
      the Position of the following element (or null, if p is last)
      Throws:
      IllegalArgumentException - if p is not a valid position for this list
    • addFirst

      public Position<E> addFirst(E e)
      Inserts an element at the front of the list.
      Specified by:
      addFirst in interface PositionalList<E>
      Parameters:
      e - the new element
      Returns:
      the Position representing the location of the new element
    • addLast

      public Position<E> addLast(E e)
      Inserts an element at the back of the list.
      Specified by:
      addLast in interface PositionalList<E>
      Parameters:
      e - the new element
      Returns:
      the Position representing the location of the new element
    • addBefore

      public Position<E> addBefore(Position<E> p, E e) throws IllegalArgumentException
      Inserts an element immediately before the given Position.
      Specified by:
      addBefore in interface PositionalList<E>
      Parameters:
      p - the Position before which the insertion takes place
      e - the new element
      Returns:
      the Position representing the location of the new element
      Throws:
      IllegalArgumentException - if p is not a valid position for this list
    • addAfter

      public Position<E> addAfter(Position<E> p, E e) throws IllegalArgumentException
      Inserts an element immediately after the given Position.
      Specified by:
      addAfter in interface PositionalList<E>
      Parameters:
      p - the Position after which the insertion takes place
      e - the new element
      Returns:
      the Position representing the location of the new element
      Throws:
      IllegalArgumentException - if p is not a valid position for this list
    • set

      public E set(Position<E> p, E e) throws IllegalArgumentException
      Replaces the element stored at the given Position and returns the replaced element.
      Specified by:
      set in interface PositionalList<E>
      Parameters:
      p - the Position of the element to be replaced
      e - the new element
      Returns:
      the replaced element
      Throws:
      IllegalArgumentException - if p is not a valid position for this list
    • remove

      public E remove(Position<E> p) throws IllegalArgumentException
      Removes the element stored at the given Position and returns it. The given position is invalidated as a result.
      Specified by:
      remove in interface PositionalList<E>
      Parameters:
      p - the Position of the element to be removed
      Returns:
      the removed element
      Throws:
      IllegalArgumentException - if p is not a valid position for this list
    • positions

      public Iterable<Position<E>> positions()
      Returns an iterable representation of the list's positions.
      Specified by:
      positions in interface PositionalList<E>
      Returns:
      iterable representation of the list's positions
    • iterator

      public Iterator<E> iterator()
      Returns an iterator of the elements stored in the list.
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface PositionalList<E>
      Returns:
      iterator of the list's elements
    • 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