Write the following classes that implement a single Linked List of integers. Cla
ID: 3845971 • Letter: W
Question
Write the following classes that implement a single Linked List of integers. Class Node: It includes two instance variables: int number and Node next It contains the following methods: A constructor that initializes the instance variable number. Set and get method for each instance variable. Class singleLinkedList: It includes two instance variables: Node head and Node. It contains the following methods: public void insert (int n) that creates and adds a Node at the end of the linked List. public void print () that prints the values of all nodes in the liked list, separated by an arrow (rightarrow), as shown in the sample output. public boolean oneDigit ()that returns true if all nodes in the linked List have a one-digit value and false otherwise. public void iterativeReverse () that reverses the original linked list iteratively. public void recursiveReverse () that calls the private method void recursiveReverse(Node head) private void recursiveReverse (Node current) that reverses the original linked list recursively. Class TestSingleLinkedList In the main method, do the following: Create an object of class singleLinkedList Read integer values from the user. The user must enter -1 to stop Call method print() Call method one Digit() and print a proper message based on the returning value. Call method recursiveReverse() and then call method print() Call method iterativeReverse() and then call method print() Sample output 1: Enter integer values or -1 to stop: 5 2 4 6 7-1 5 rightarrow 2 rightarrow 4 rightarrow 6 rightarrow 7 All values in LinkedList are one-digit integers Reversing the LinkedList using recursive method: 7 rightarrow 6 rightarrow 4 rightarrow 2 rightarrow 5 Reversing the LinkedList again using iterative method: Sample output 2. Enter integer values or -1 to stop: 10 7 3 2 4 -1 10 rightarrow 7 rightarrow 3 rightarrow 2 rightarrow 4 Not all values in LinkedList are one-digit integers Reversing the LinkedList using recursive method: 4 rightarrow 2 rightarrow 3 rightarrow 7 rightarrow 18 Reversing the LinkedList again using iterative method: 10 rightarrow 7 rightarrow 3 rightarrow 2 rightarrow 4Explanation / Answer
import java.util.Scanner;
class Node
{
protected int data;
protected Node link;
public Node()
{
link = null;
data = 0;
}
public Node(int d,Node n)
{
data = d;
link = n;
}
public void setLink(Node n)
{
link = n;
}
public void setData(int d)
{
data = d;
}
public Node getLink()
{
return link;
}
public int getData()
{
return data;
}
}
class SinglelinkedList
{
protected Node head;
protected Node tail;
public int size ;
/* Constructor */
public linkedList()
{
head= null;
tail= null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return head== null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert an element at end */
public void insert(int val)
{
Node nptr = new Node(val,null);
size++ ;
if(head == null)
{
head = nptr;
tail = start;
}
else
{
tail.setLink(nptr);
tail = nptr;
}
}
/* Function to display elements */
public void display()
{
System.out.print(" Singly Linked List = ");
if (size == 0)
{
System.out.print("empty ");
return;
}
if (head.getLink() == null)
{
System.out.println(head.getData() );
return;
}
Node ptr = head;
System.out.print(head.getData()+ "->");
ptr = head.getLink();
while (ptr.getLink() != null)
{
System.out.print(ptr.getData()+ "->");
ptr = ptr.getLink();
}
System.out.print(ptr.getData()+ " ");
}
}
public boolean isSingleDigit (int x)
{ Node ptr = head;
ptr = head.getLink();
while (ptr.getLink() != null)
{
return (ptr.getData() >= 0 && ptr.getData() < 10);
}
}
public void Iterativereverse(Node currentNode)
{
Node previousNode=null;
Node nextNode;
while(currentNode!=null)
{
nextNode=currentNode.next;
currentNode.next=previousNode;
previousNode=currentNode;
currentNode=nextNode;
}
return previousNode;
}
private void recursiveReverse(Node current) {
if (current== null || current.next == null) {
return current;
}
Node remaining = reverseLinkedList(current.next);
current.next.next = current;
current.next = null;
return remaining;
}
public void recursiveReverse ()
{ recursiveReverse (current)
}
public class TestSingleLinkedList
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedList */
linkedList list = new linkedList();
System.out.println("Singly Linked List Test ");
char ch;
/* Perform list operations */
do
{
System.out.println(" Singly Linked List Operations ");
System.out.println("1. insert at end");
System.out.println("2. check empty");
System.out.println("3. get size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
int num = scan.nextInt() ;
System.out.println("Enter position");
int pos = scan.nextInt() ;
if (pos <= 1 || pos > list.getSize() )
System.out.println("Invalid position ");
else
list.insertAtPos(num, pos);
break;
case 2 :
System.out.println("Empty status = "+ list.isEmpty());
break;
case 6 :
System.out.println("Size = "+ list.getSize() +" ");
break;
default :
System.out.println("Wrong Entry ");
break;
}
/* Display List */
list.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch != -1);
}
}
list.print();
int a=list.OneDigit()
{ if (a!=0)
{ System.out.println("Single Digit value");}
list.RecursiveReverse()
{ list.print();
}
list.IterativeReverse()
{ list.print();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.