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

This programme is done by an chegg expert.. Can anyone can explain the whole cod

ID: 3881846 • Letter: T

Question

This programme is done by an chegg expert..

Can anyone can explain the whole code and how it's work? Please answer as soon as possible...

/***Save these Three files compile them with javac and run PriorityQueueTest.java file***/
/****************************
Node.java
****************************/

public class Node {
   public int data;
   public Node next;

   public Node(int data) {
       this.data = data;
   }
}

/****************************
SortedLinkedList.java
****************************/

public class SortedLinkedList {
   public Node head;
   public void offer(int data) {
       Node node = new Node(data);
       if(head==null){
           head=node;
           return;
       }
       insert(node);
   }
   public int poll(){
       if(head==null)
           throw new RuntimeException("Priority Queue Empty");
       int data =head.data;
       head=head.next;
       return data;
   }
   public String toString() {
       StringBuffer sb = new StringBuffer();
       Node itr = head;
       while (itr != null) {
           sb.append(itr.data + " ");
           itr = itr.next;
       }
       return sb.toString();
   }

   private void insert(Node newNode){
       if(head.data>newNode.data){
           newNode.next=head;
           head=newNode;
           return;
       }
       Node iterator=head.next;
       Node previous=head;
       while((iterator!=null) && (iterator.data<=newNode.data)){
           previous=iterator;
           iterator=iterator.next;
       }
       newNode.next=iterator;
       previous.next=newNode;
   }
}

/****************************
PriorityQueueTest.java
****************************/
public class PriorityQueueTest {
   public static void main(String[] args){
       SortedLinkedList queue=new SortedLinkedList();
       queue.offer(12);
       queue.offer(10);
       queue.offer(1);
       queue.offer(23);
       queue.offer(6);
       queue.offer(11);
      
       System.out.println(queue.poll());
       System.out.println(queue.poll());
       System.out.println(queue.poll());
       System.out.println(queue.poll());
       System.out.println(queue.poll());
       System.out.println(queue.poll());
      
   }
}

Explanation / Answer

There are mainly three class are there

In the first class of the java, the initialization of the linked list takes place. In this, the node is created and data are provided to the Node.

In the second class, the SortedLinkedList four methods are provided.

1. public void offer(int data) {
Node node = new Node(data); // New node is created
if(head==null) // condition if head is null then make the new node as the head of the linked list.

{
head=node;
return;
}
insert(node); // call the function insert. to check where insert the new node.
}

2. I'm now just going to the insert fuction as it is called in the previous function so it will be easy to understand.

private void insert(Node newNode){
if(head.data>newNode.data){ // comparison the data of new node to the head. If the head is greater then new // node the make the new node as the head.
newNode.next=head;
head=newNode;
return;
}
Node iterator=head.next;
Node previous=head;
while((iterator!=null) && (iterator.data<=newNode.data)){ // Searching the node for the appropriate value of the // new node and to find the position to place the new node. This loop // will run till it find the node that is greater then new node.
previous=iterator;
iterator=iterator.next;
}
newNode.next=iterator;
previous.next=newNode;
}

3.  public int poll(){
if(head==null)
throw new RuntimeException("Priority Queue Empty");
int data =head.data;
head=head.next;
return data;

This 3rd function int poll() will just takes the data from the start of the list and return it and increase the head to the next element. So that next value of the head will print on the next line.

4. String toString() // this function as of I think is not required in the program as there is no call for this function in the entire program.

In the third java class PriorityQueueTest.java there is the main method declared and all the functions are called first to insert the value then to print the values.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote