Package com.zybooks.dsaj.list
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>
Implementation of a positional list stored as a doubly linked list.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionInserts an element immediately after the given Position.Inserts an element immediately before the given Position.Inserts an element at the front of the list.Inserts an element at the back of the list.Returns the Position immediately after Position p.Returns the Position immediately before Position p.first()Returns the first Position in the list.booleanisEmpty()Tests whether the list is empty.iterator()Returns an iterator of the elements stored in the list.last()Returns the last Position in the list.Returns an iterable representation of the list's positions.Removes the element stored at the given Position and returns it.Replaces the element stored at the given Position and returns the replaced element.intsize()Returns the number of elements in the list.toString()Produces a string representation of the contents of the list.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
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:
sizein interfacePositionalList<E>- Returns:
- number of elements in the list
-
isEmpty
public boolean isEmpty()Tests whether the list is empty.- Specified by:
isEmptyin interfacePositionalList<E>- Returns:
- true if the list is empty, false otherwise
-
first
Returns the first Position in the list.- Specified by:
firstin interfacePositionalList<E>- Returns:
- the first Position in the list (or null, if empty)
-
last
Returns the last Position in the list.- Specified by:
lastin interfacePositionalList<E>- Returns:
- the last Position in the list (or null, if empty)
-
before
Returns the Position immediately before Position p.- Specified by:
beforein interfacePositionalList<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
Returns the Position immediately after Position p.- Specified by:
afterin interfacePositionalList<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
Inserts an element at the front of the list.- Specified by:
addFirstin interfacePositionalList<E>- Parameters:
e- the new element- Returns:
- the Position representing the location of the new element
-
addLast
Inserts an element at the back of the list.- Specified by:
addLastin interfacePositionalList<E>- Parameters:
e- the new element- Returns:
- the Position representing the location of the new element
-
addBefore
Inserts an element immediately before the given Position.- Specified by:
addBeforein interfacePositionalList<E>- Parameters:
p- the Position before which the insertion takes placee- 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
Inserts an element immediately after the given Position.- Specified by:
addAfterin interfacePositionalList<E>- Parameters:
p- the Position after which the insertion takes placee- 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
Replaces the element stored at the given Position and returns the replaced element.- Specified by:
setin interfacePositionalList<E>- Parameters:
p- the Position of the element to be replacede- the new element- Returns:
- the replaced element
- Throws:
IllegalArgumentException- if p is not a valid position for this list
-
remove
Removes the element stored at the given Position and returns it. The given position is invalidated as a result.- Specified by:
removein interfacePositionalList<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
Returns an iterable representation of the list's positions.- Specified by:
positionsin interfacePositionalList<E>- Returns:
- iterable representation of the list's positions
-
iterator
Returns an iterator of the elements stored in the list. -
toString
Produces a string representation of the contents of the list. This exists for debugging purposes only.
-