Nodes are a basic data structure which contain data and one or more links to other nodes. Nodes can be used to represent a tree structure or a linked list. In such structures where nodes are used, it is possible to traverse from one node to another node.
Nodes that have no links pointing to them except for the head node, are considered “orphaned.” In the illustration, if the nodes a2
and a5
are removed, they will be orphaned.
Data structures containing nodes have typically two bits of information stored in a node: data and link to next node.
The first part is a value and the second part is an address of sorts pointing to the next node. In this way, a system of nodes is created. A NULL
value in the link part of a node’s info denotes that the path or data structure contains no further nodes.
A Node class in Java has the following attributes:
public
String
data
and private
Node
next
instance variablesString
data
as an argument and sets the data
instance variable to it as well as sets next
to null
.setNextNode()
to set the next
property.getNextNode()
to access the next
propertypublic class Node {public String data;private Node next;public Node(String data) {this.data = data;this.next = null;}public void setNextNode(Node node) {this.next = node;}public Node getNextNode() {return this.next;}}
A Node
constructor should take in data
as a variable. Our implementation had data
as a String
, but it could also be an int
or float
, etc. The constructor should then:
data
instance variable to the passed in data
variablenext
instance variable to null
public Node(String data) {this.data = data;this.next = null;}
A Java Node
class has a .setNextNode()
method that takes in a Node
and updates the next
instance variable appropriately.
public void setNextNode(Node node) {this.next = node;}
A Java Node
class has a .getNextNode()
method with no parameters that returns the next
class property.
public Node getNextNode() {return this.next;}