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

I NEED SOMETHING TO COPY/PASTE AND COMPILE. IT ALSO MUST PASS BOTH DRIVERS AND M

ID: 3539455 • Letter: I

Question

I NEED SOMETHING TO COPY/PASTE AND COMPILE. IT ALSO MUST PASS BOTH DRIVERS AND MAKE SURE YOU USE COMMENTS// EXPLAINING YOUR CODE! THANKS!


For this homework, you will write two methods, each method will go into the

LLMethods2.java file. Be sure to include the following file in the same directory as LLMethods2:

The first method you will write will have the following header

This method will recursively insert a thing (value) into a linked list (list) in order. Be sure to consider the case where list is empty.

The psuedocode for this method is:

.

The second method has the following header:

This method will take a linked list (list) in which the values are in any order and return a list in which the elements are in sorted order.

The psuedocode for this method is:

. .

Explanation / Answer

    import java.io.*;
    class Node
    {
       public int data;
       public Node next;
       public Node(int x)
       {
         data=x;
       }
       public void displayNode()
       {
         System.out.print(data+" ");
       }
    }
    class LinkList
    {
       private Node first;
       private Node last;
       public LinkList()
       {
         first=null;
         last=null;
       }
       public void insertLast(int x)
       {
         Node newNode=new Node(x);
         newNode.next=null;
         if(isEmpty())
           first=newNode;
         else
           last.next=newNode;
         last=newNode;
       }
       public int deleteFirst()
       {
         int t=first.data;
         if(first.next==null)
           last=null;
         first=first.next;
         return t;
       }
      
       public boolean isEmpty()
       {
         return(first==null);
       }
       public void displayList()
       {
         Node current=first;
         while(current!=null)
         {
           current.displayNode();
           current=current.next;
         }
       }
    }
    class Queue
    {
       private LinkList l;
       public Queue()
       {
         l=new LinkList();
       }
       public void enqueue(int x)
       {
         l.insertLast(x);
         System.out.print("Inserted");
       }
       public int dequeue()
       {
         return l.deleteFirst();
       }
       public boolean isQueueEmpty()
       {
         return l.isEmpty();
       }
       public void display()
       {
         l.displayList();
       }
      
    }
    class DriverQ
    {
       public static void main(String args[])throws IOException
       {
         String ch="y";
         DataInputStream inp=new DataInputStream(System.in);
         int n,d;
         Queue q=new Queue();
        q.enqueue(10);
        System.out.println("10 inserted into queue...");
        q.enqueue(20);
        System.out.println("20 inserted into queue...");
        q.enqueue(30);
        System.out.println("30 inserted into queue...");
        q.enqueue(40);
        System.out.println("40 inserted into queue...");
        System.out.println(" Queue values after insertion are:------ ");
        q.display();
       
        q.dequeue();
        System.out.println(" Dequeue function called");
        System.out.println("Queue values after Dequeue called are:------ ");
        q.display();
         
          
       }
    }