Package com.Roukan.datastructures.list
Class DoublyNode<T>
java.lang.Object
com.Roukan.datastructures.list.DoublyNode<T>
- Type Parameters:
T
- the type of value stored in this node
Represents a node in a doubly linked list.
Each node contains a value and references to both the next and previous nodes in the sequence,
allowing bidirectional traversal.
- Since:
- 2024
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new empty doubly node with null value and null references.DoublyNode
(T value) Constructs a new doubly node with the specified value and null references for both next and previous nodes.DoublyNode
(T value, DoublyNode<T> nextNode, DoublyNode<T> previousNode) Constructs a new doubly node with the specified value, next node, and previous node. -
Method Summary
Modifier and TypeMethodDescriptionReturns the next node in the sequence.Returns the previous node in the sequence.getValue()
Returns the value stored in this node.void
setNextNode
(DoublyNode<T> nextNode) Sets the reference to the next node in the sequence.void
setPreviousNode
(DoublyNode<T> previousNode) Sets the reference to the previous node in the sequence.void
Sets a new value for this node.toString()
Returns a string representation of this node's value.
-
Constructor Details
-
DoublyNode
Constructs a new doubly node with the specified value and null references for both next and previous nodes. This constructor is typically used for creating isolated nodes or the first node in a list.- Parameters:
value
- the value to be stored in this node
-
DoublyNode
Constructs a new doubly node with the specified value, next node, and previous node. This constructor is used for creating nodes that are fully connected in both directions.- Parameters:
value
- the value to be stored in this nodenextNode
- the next node in the sequencepreviousNode
- the previous node in the sequence
-
DoublyNode
public DoublyNode()Constructs a new empty doubly node with null value and null references. This constructor creates a node that can be initialized later. Use with caution as the value is null.
-
-
Method Details
-
getValue
Returns the value stored in this node.- Returns:
- the value stored in this node, may be null if default constructor was used
-
setValue
Sets a new value for this node.- Parameters:
value
- the new value to be stored in this node
-
setNextNode
Sets the reference to the next node in the sequence.- Parameters:
nextNode
- the next node in the sequence, or null to make this the last node
-
getNextNode
Returns the next node in the sequence. If this node is the last one in the list, returns null.- Returns:
- the next node in the sequence, or null if this is the last node
-
getPreviousNode
Returns the previous node in the sequence. If this node is the first one in the list, returns null.- Returns:
- the previous node in the sequence, or null if this is the first node
-
setPreviousNode
Sets the reference to the previous node in the sequence.- Parameters:
previousNode
- the previous node in the sequence, or null to make this the first node
-
toString
Returns a string representation of this node's value. Calls toString() on the stored value. If the value is null, this method will throw a NullPointerException.- Overrides:
toString
in classObject
- Returns:
- a string representation of the value stored in this node
- Throws:
NullPointerException
- if the value is null
-