java linked list
//Insert, Show Nodes - java-LinkedList
public class Node {
int data;
Node next;
//main
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.insert(9);
list.insert(10);
list.insert(15);
list.insert(19);
list.show();
}
}
//
class LinkedList {
Node head;
//insert a node - method
public void insert(int data) {
Node node = new Node();
node.data = data;
node.next = null;
//check if node is single
if (head == null) {
head = node;
} else {
// go to the end of linkedList
Node n = head;
while (n.next != null) {
n = n.next;
}
//set the last node to be node
n.next = node;
}
}
//print the list of nodes
public void show() {
Node node = head;
//iterate and print
while (node.next != null) {
System.out.println(node.data);
node = node.next;
}
//print the last one that the node.next == null;
System.out.println(node.data);
}
}