Package dsa.lab02.base
Interface LinkedNode<Item>
- Type Parameters:
Item
- the item type
- All Known Implementing Classes:
DoublyLinkedList.Node
,DoublyLinkedList.Node
,SinglyLinkedList.Node
,SinglyLinkedList.Node
public interface LinkedNode<Item>
A node in a linked list.
Depending on the specific type, many operations may be O(1) or O(n
)
(where n
is the size of the containing list).
Nodes are only valid as long as they remain in a list -
if removed from that list, they should no longer be used.
-
Method Summary
Modifier and TypeMethodDescriptiondefault boolean
hasNext()
Check if it has a next node.default boolean
Check if it has a previous node.void
insertNext
(Item item) Insert a node containing the given item immediately after this one in the list.void
insertPrevious
(Item item) Insert a node containing the given item immediately before this one in the list.default boolean
isFirst()
Check if it's the first in the list.default boolean
isLast()
Check if it's the last in the list.item()
Get the contained item.list()
Get the containing list.next()
Get the next node.previous()
Get the previous node.remove()
Remove the node from the list and return its item.Remove the next node from the list and return its item.Remove the previous node from the list and return its item.void
Set the contained item.
-
Method Details
-
list
LinkedList<Item> list()Get the containing list.- Returns:
- the list
-
item
Item item()Get the contained item.- Returns:
- the item
-
setItem
Set the contained item.Replaces whatever was contained before.
- Parameters:
item
- the new item
-
previous
LinkedNode<Item> previous()Get the previous node. ornull
if this is the first.- Returns:
- the predecessor
-
next
LinkedNode<Item> next()Get the next node. ornull
if this is the last.- Returns:
- the successor
-
insertPrevious
Insert a node containing the given item immediately before this one in the list.- Parameters:
item
- the new previous item
-
insertNext
Insert a node containing the given item immediately after this one in the list.- Parameters:
item
- the new next item
-
remove
Item remove()Remove the node from the list and return its item.- Returns:
- the item
-
removePrevious
Remove the previous node from the list and return its item.- Returns:
- the old previous item
- Throws:
NoSuchElementException
- if there is no previous node (i.e. this is the first)
-
removeNext
Remove the next node from the list and return its item.- Returns:
- the old next item
- Throws:
NoSuchElementException
- if there is no next node (i.e. this is the last)
-
isFirst
default boolean isFirst()Check if it's the first in the list.- Returns:
- whether there are no predecessors
-
isLast
default boolean isLast()Check if it's the last in the list.- Returns:
- whether there are no successors
-
hasPrevious
default boolean hasPrevious()Check if it has a previous node.- Returns:
- whether it's not the first
-
hasNext
default boolean hasNext()Check if it has a next node.- Returns:
- whether it's not the last
-