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 Type
    Method
    Description
    default boolean
    Check if it has a next node.
    default boolean
    Check if it has a previous node.
    void
    Insert a node containing the given item immediately after this one in the list.
    void
    Insert a node containing the given item immediately before this one in the list.
    default boolean
    Check if it's the first in the list.
    default boolean
    Check if it's the last in the list.
    Get the contained item.
    Get the containing list.
    Get the next node.
    Get the previous node.
    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
    setItem(Item item)
    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

      void setItem(Item item)
      Set the contained item.

      Replaces whatever was contained before.

      Parameters:
      item - the new item
    • previous

      LinkedNode<Item> previous()
      Get the previous node. or null if this is the first.
      Returns:
      the predecessor
    • next

      LinkedNode<Item> next()
      Get the next node. or null if this is the last.
      Returns:
      the successor
    • insertPrevious

      void insertPrevious(Item item)
      Insert a node containing the given item immediately before this one in the list.
      Parameters:
      item - the new previous item
    • insertNext

      void insertNext(Item item)
      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

      Item removePrevious() throws NoSuchElementException
      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

      Item removeNext() throws NoSuchElementException
      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