Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the following classes that implement a single Linked List of integers: Cla

ID: 3851504 • 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 Single Linked List: It includes two instance variables: Node head and Node tail. 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 finked list, separated by an arrow (rightarrow). as shown in the sample output public Boolean one Digit () that returns true if all nodes in the linked List have a one-digit value and false otherwise public void iterative Reverse () that reverses the original linked list iteratively. public void recursive Reverse () that calls the private method void recursive Reverse (Node head) private void recursive Reverse (Node current) that reverses the original linked list recursively. Class Test Single Linked List in the main method, do the following: Create an object of class Single Unwed List 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 recursive Reverse () and then call method print() Call method iterative Reverse () and then call method print() 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 Linked List are one-digit integers Reversing the Linked List using recursive method:

Explanation / Answer

Answer -> I write this program according to the requirements of the question .

--------------------------------------------------------------------------------------------------------------------------------------------------------

package my_chegg_package;
import java.util.Scanner;
public class TestSingleLinkedList {

  
   public static void main(String[] args) {
      
               Scanner sc=new Scanner(System.in);
             
               SingleLinkedList obj = new SingleLinkedList();
             
               System.out.println("enter integer values or -1 to stop ");
             
               do{
                   int number = sc.nextInt();
                   if(number != -1)
                   {
                       obj.insert(number);
                   }
                   else if(number == -1)
                   {
                       break;
                   }
               }
                   while(true);
             
               System.out.println("Single Linked List is ");
             
               obj.print();
             
               if(obj.oneDigit())
               {
                   System.out.println(" All values in the linked list are one-digit numbers ");
               }
               else
               {
                   System.out.println(" All values in the linked list are not one-digit numbers ");
               }
             
               System.out.println(" Single Linked List after reverse ");
             
               obj.iterativeReverse();
             
               obj.print();
             
               }
   }


---------------------------------------------------------------------------------------------------------------------------------------------------

package my_chegg_package;

public class SingleLinkedList {
   private Node head;
   private Node tail;
   public SingleLinkedList()
   {
       head=null;
       tail=null;
   }
  
    public void insert(int n)
    {

        Node ptr = new Node(n);     
        if(head == null)
        {
            head = ptr;
            tail = head;
        }
        else
        {
            tail.setNext(ptr);
            tail =ptr;
        }

    }
    public void print()
    {
        if (head.getNext() == null)
        {
            System.out.println(head.getNumber() );
            return;
        }
        Node ptr = head;
        System.out.print(head.getNumber()+ "->");
        ptr = head.getNext();
        while (ptr.getNext() != null)
        {
            System.out.print(ptr.getNumber()+ "->");
            ptr = ptr.getNext();
        }
        System.out.print(ptr.getNumber()+ " ");
    }
  
    public boolean oneDigit()
    {
       Node ptr = head;
         ptr = head.getNext();
         while (ptr.getNext() != null)
         {
           if((-9 > ptr.getNumber()) || (ptr.getNumber() > 9))
           {
               return false;
           }
             ptr = ptr.getNext();
         }
         if((-9 > ptr.getNumber()) || (ptr.getNumber() > 9))
       {
           return false;
       }
       return true;
    }
  
    public void iterativeReverse()
    {
       Node b = null;
       Node temp = head;
       Node next=null;
       while (temp != null) {
             next = temp.getNext();
             temp.setNext(b);
             b = temp;
             temp = next;
       }
       head = b;
            }
          
          
        }
  

---------------------------------------------------------------------------------------------------------------------------------------------

package my_chegg_package;

public class Node {
   private int number;
   private Node next;
   public Node(int number)
   {
       this.number=number;
       next = null;
   }
   public int getNumber() {
       return number;
   }
   public void setNumber(int number) {
       this.number = number;
   }
   public Node getNext() {
       return next;
   }
   public void setNext(Node next) {
       this.next = next;
   }
  
  
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

OUTPUT ->

enter integer values or -1 to stop

5
3
6
1
7
-1


Single Linked List is

5->3->6->1->7

All values in the linked list are one-digit numbers


Single Linked List after reverse

7->1->6->3->5