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

Using object references to do linking. The lab assignment: In this lab you are t

ID: 3889921 • Letter: U

Question

Using object references to do linking.

The lab assignment:

In this lab you are to implement a sorted list with the doubly linked technique. You are required

to use the following DLNode class.

public class DLNode {

double data;

DLNode Prev= null, Next=null;

}

1. Download instructor’s SortedList.java, DLNode.class and lab04.class.

2. Complete program SortedList.java by implementing the following methods.

a. insert(double n) – Insert double precision number n into the sorted list.

b. listAscending() – List all numbers of the list in ascending order.

c. listDescending() – List all numbers of the list in descending order.

3. Compile SortedList.java and run lab04.class.

4. Try the output:

Numbers in ascending order

0.005453 0.015824 0.034760 0.035252 0.045030

0.059204 0.059580 0.064386 0.066729 0.068687

0.070243 0.088476 0.113436 0.126505 0.138185

0.167384 0.174744 0.180848 0.186196 0.211579

0.216779 0.234507 0.239412 0.252426 0.272560

0.273805 0.284689 0.293194 0.298804 0.317749

0.330338 0.345510 0.349787 0.376190 0.381064

0.384965 0.409338 0.422811 0.426991 0.427279

0.429292 0.430055 0.436518 0.460323 0.473449

0.477191 0.490301 0.504000 0.504488 0.524943

0.540148 0.551920 0.556803 0.596579 0.603876

0.616740 0.638223 0.641767 0.648372 0.651566

0.658112 0.659656 0.669204 0.674729 0.675603

0.678083 0.716932 0.727070 0.727376 0.742798

0.751170 0.752763 0.754303 0.757124 0.757623

0.758995 0.777728 0.781542 0.796080 0.800124

0.806681 0.812600 0.830753 0.832565 0.838959

0.841686 0.842588 0.872757 0.881787 0.885562

0.899885 0.914564 0.915994 0.938268 0.941031

0.942194 0.954282 0.977334 0.994594 0.999837

Numbers in descending order

0.999837 0.994594 0.977334 0.954282 0.942194

0.941031 0.938268 0.915994 0.914564 0.899885

0.885562 0.881787 0.872757 0.842588 0.841686

0.838959 0.832565 0.830753 0.812600 0.806681

0.800124 0.796080 0.781542 0.777728 0.758995

0.757623 0.757124 0.754303 0.752763 0.751170

0.742798 0.727376 0.727070 0.716932 0.678083

0.675603 0.674729 0.669204 0.659656 0.658112

0.651566 0.648372 0.641767 0.638223 0.616740

0.603876 0.596579 0.556803 0.551920 0.540148

0.524943 0.504488 0.504000 0.490301 0.477191

0.473449 0.460323 0.436518 0.430055 0.429292

0.427279 0.426991 0.422811 0.409338 0.384965

0.381064 0.376190 0.349787 0.345510 0.330338

0.317749 0.298804 0.293194 0.284689 0.273805

0.272560 0.252426 0.239412 0.234507 0.216779

0.211579 0.186196 0.180848 0.174744 0.167384

0.138185 0.126505 0.113436 0.088476 0.070243

0.068687 0.066729 0.064386 0.059580 0.059204

0.045030 0.035252 0.034760 0.015824 0.005453

Also : Ues: SortedList. java

public class SortedList {

                SortedList() {

                                System.out.println("Instructor's Test Sorted Class");

                }

                private DLNode Head, Tail;

                public void insert(double n){

                }

                public void listAscending() {

                }

                public void listDescending() {

                }

}

Explanation / Answer

Given below is the completed SortedList class. I have tested it the TestSortedList. You may test with your lab04 file. Hope the anwer helps. If it helped, please don't forget to rate the answer . Thank you very much.

public class SortedList {

SortedList() {

   System.out.println("Instructor's Test Sorted Class");

}

  

private DLNode Head, Tail;

public void insert(double n){

   DLNode newnode = new DLNode();

   newnode.data = n;

   newnode.Next = null;

   newnode.Prev = null;

   if(Head == null) //empty list

       Head = Tail = newnode;

   else

   {

       DLNode curr = Head, prev = null;

          

       //find the correct place to insert

       while(curr != null)

       {

           if(curr.data > n) //if we reach a node which is higher than data to be inserted

               break;

           prev = curr;

           curr = curr.Next;

       }

          

          

       if(curr == Head) //should insert before head?

       {

           //establish the 2 links between new node and Head node which should come after its

           newnode.Next = Head;

           Head.Prev = newnode;

           Head = newnode; //make the new node head

       }

       else

       {

           //the newnode will be inserted in between prev and curr

              

           //establish 4 links -

           //prev ----> newnode

           //newnode -----> curr

           //newnode <------- curr (if curr != null)

           //prev <------ newnode

              

           prev.Next = newnode;

           newnode.Next = curr;

           if(curr != null)

               curr.Prev = newnode;

           newnode.Prev = prev;

       }

          

       if(prev == Tail) //insertion was at end of list, update Tail

           Tail = newnode;

   }

}

public void listAscending() {

   DLNode curr = Head;

   while(curr != null)

   {

       System.out.println(curr.data);

       curr = curr.Next;

   }

}

public void listDescending() {

   DLNode curr = Tail;

   while(curr != null)

   {

       System.out.println(curr.data);

       curr = curr.Prev;

   }

}

}

====================

Program used to test SortedList

public class TestSortedList {

   public static void main(String[] args) {

       SortedList list = new SortedList();

       list.insert(4.5);

       list.insert(2.9);

       list.insert(3.5);

       list.insert(6.0);

       list.insert(1.0);

       System.out.println("Ascending order");

       list.listAscending();

       System.out.println("Descending order");

       list.listDescending();

      

   }

}

output

Instructor's Test Sorted Class
Ascending order
1.0
2.9
3.5
4.5
6.0
Descending order
6.0
4.5
3.5
2.9
1.0

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