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

Write a method called isSorted that returns true if the list is in sorted (nonde

ID: 3568803 • Letter: W

Question

Write a method called isSorted that returns true if the list is in sorted (nondecending) order and returns false otherwise. An empty list is considered to be sorted. Please implement the list [4, 5, 9, 20, 34, 87]. The method should be added to this LinkedIntList class.

public class LinkedIntList {
       private ListNode front;
      
       public LinkedIntList() ;
           front = null;
       }
       public int size() {
               int count = 0;
               ListNode current = front;
               while (current != null) {
                       current - current.next;
                       count++;
                   }
                   return count;
               }
       public int get(int index) {
               return nodeAt(index).data;
           }
       public String toString() {
               if (front == null) {
                       return "[]";
                   } else {
                           String result = "[" + front.data;
                           ListNode current = front.next;
                           while (current != null) {
                                   result += ", " + current.data;
                                   current = current.next;
                               }
                               result += "]";
                               return result;
                           }
       }
       public int indexOf(int value) {
               int index = 0;
               ListNode current = front;
               while (current != null) {
                       if (current.data == value) {
                               return index;
                           }
                           index++;
                           current = current.next;
                       }
                       return -1;
       }
       public void add(int value) {
               if (front == null) {
                       front = new ListNode(value);
                   } else {
                           ListNode current = front;
                           while (current.next != null) {
                                   current = current.next;
                           }
                           current.next = new ListNode(value);
                   }
       }
       public void add(int index, int value) {
               if (index == 0) {
                       front = new ListNode(value, front);
               } else {
                       ListNode current = nodeAt(index - 1);
                       current.next = new ListNode(value, current.next);
               }
       }
       public void remove(int index) {
               if (index == 0) {
                       front = front.next;
               } else {
                       ListNode current = nodeAt(index - 1);
                       current.next = current.next.next;
               }
       }
       private ListNode nodeAt(int index) {
               ListNode current = front;
               for (int i = 0; i < index; i++) {
                       current = current.next;
               }
               return current;
       }
   }

Explanation / Answer

//Note : Add this method to the LinkedIntList class

//isSorted() method returns true if the list is in sorted

public boolean isSorted()
{
   boolean result = true;
   if(size() != 0 && size() != 1) // empty list is considered to be sorted
   {
       for(int i = 0; i < size() - 1; i++)
       {
           if(get(i) > get(i+1)) // if any of 2 elements are in decreasing order, breaks loop and returns false
           {
               result = false;
               break;
           }
       }
   }
   return result;
}

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