In: Computer Science
In java please create a class with a method to be able to add ints into a linked List and to be able to print out all the values at the end.
public class Node {
int data;
Node next;
Answer :
The required java code :
public class SinglyLinkedList {
class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public Node head = null;
public Node tail = null;
public void addNode(int data) {
Node newNode = new Node(data);
if(head == null) {
head = newNode;
tail = newNode;
}
else {
tail.next = newNode;
tail = newNode;
}
}
public void display() {
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Nodes of singly linked list: ");
while(current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public static void main(String[] args) {
SinglyLinkedList sList = new SinglyLinkedList();
sList.addNode(1);
sList.addNode(2);
sList.addNode(3);
sList.addNode(4);
sList.display();
}
}
Result :
=============================================================================
ANSWERED AS PER MY KNOWLEDGE
IF ANY DOUBT COMMENT IT