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
-
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.boolean
isEmpty()
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.int
size()
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, wait
Methods 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:
size
in interfacePositionalList<E>
- Returns:
- number of elements in the list
-
isEmpty
public boolean isEmpty()Tests whether the list is empty.- Specified by:
isEmpty
in interfacePositionalList<E>
- Returns:
- true if the list is empty, false otherwise
-
first
Returns the first Position in the list.- Specified by:
first
in interfacePositionalList<E>
- Returns:
- the first Position in the list (or null, if empty)
-
last
Returns the last Position in the list.- Specified by:
last
in interfacePositionalList<E>
- Returns:
- the last Position in the list (or null, if empty)
-
before
Returns the Position immediately before Position p.- Specified by:
before
in 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:
after
in 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:
addFirst
in 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:
addLast
in 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:
addBefore
in 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:
addAfter
in 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:
set
in 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:
remove
in 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:
positions
in 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.
-