Question

In: Computer Science

// priorityList.java // a priority queue based on a sorted list // to run this program:...

// priorityList.java
// a priority queue based on a sorted list
// to run this program: C>java PiorityQApp
////////////////////////////////////////////////////////////////
class Link
   {
   public long dData;                  // data item
   public Link next;                   // next link in list
// -------------------------------------------------------------
   public Link(long dd)                // constructor
      { dData = dd; }
// -------------------------------------------------------------
   public void displayLink()           // display this link
      { System.out.print(dData + " "); }
   }  // end class Link
////////////////////////////////////////////////////////////////
class SortedList
   {
   private Link first;                 // ref to first item
// -------------------------------------------------------------
   public SortedList()                 // constructor
      { first = null; }
// -------------------------------------------------------------
   public boolean isEmpty()            // true if no links
      { return (first==null); }
// -------------------------------------------------------------
   public void insert(long key)        // insert, in order
      {
      Link newLink = new Link(key);    // make new link
      Link previous = null;            // start at first
      Link current = first;
                                       // until end of list,
      while(current != null && key > current.dData)
         {                             // or key > current,
         previous = current;
         current = current.next;       // go to next item
         }
      if(previous==null)               // at beginning of list
         first = newLink;              // first --> newLink
      else                             // not at beginning
         previous.next = newLink;      // old prev --> newLink
      newLink.next = current;          // newLink --> old currnt
      }  // end insert()
// -------------------------------------------------------------
   public Link remove()           // return & delete first link
      {                           // (assumes non-empty list)
      Link temp = first;               // save first
      first = first.next;              // delete first
      return temp;                     // return value
      }
// -------------------------------------------------------------
   public void displayList()
      {
      Link current = first;       // start at beginning of list
      while(current != null)      // until end of list,
         {
         current.displayLink();   // print data
         current = current.next;  // move to next link
         }
      }
   }  // end class SortedList
////////////////////////////////////////////////////////////////
class PriorityQ
   {
   private SortedList theList;
//-------------------------------------------------------------
   public PriorityQ()               // constructor
      {
      theList = new SortedList();
      }
//-------------------------------------------------------------
   public void insert(long item)    // insert item
      {
           // TBD - NEED TO DO INSERT HERE
      }
//-------------------------------------------------------------
   public long remove()             // remove minimum item
      {
          // TBD - THIS SHOULD REMOVE THE FIRST ITEM FROM AND LIST AND RETURN THE DATA VALUE OF THE LINK REMOVED
      }
//-------------------------------------------------------------
   public boolean isEmpty()         // true if queue is empty
      {
         // TBD - THIS SHOULD RETURN WHETHER THE QUEUE IS EMPTY OR NOT
      }
//-------------------------------------------------------------
   }  // end class PriorityQ
////////////////////////////////////////////////////////////////
class PriorityQApp
   {
   public static void main(String[] args)
      {
      PriorityQ thePQ = new PriorityQ();

      thePQ.insert(30);
      thePQ.insert(50);
      thePQ.insert(10);
      thePQ.insert(40);
      thePQ.insert(20);

      while( !thePQ.isEmpty() )
         {
         long item = thePQ.remove();
         System.out.print(item + " ");  // 10, 20, 30, 40, 50
         }  // end while
      System.out.println("");
      }  // end main()
//-------------------------------------------------------------
   }  // end class PriorityQApp

Implement a priority queue based on a sorted linked list.

I have provided the code to get started in file priorityList.java.  This code consists of the sorttedlist.java program from listing 5.6, along with the main app class (class PriorityQApp) from the PriorityQ.java from Listing 4.6.  I have added a shell class PriorityQ, which will be the ADT, as this will implement a Priority Queue based on the underlying sorted linked list.You assignment will be to then add code to the 3 methods from within the class PriorityQ (the methods have a comment "// TBD") - to make it all work.  Once you add the methods, you should test it to verify it works.Note, that the remove operation on the priority queue should remove the item with the smallest key.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks


class Link {
        public long dData; // data item
        public Link next; // next link in list

        // -------------------------------------------------------------

        public Link(long dd) // constructor
        {
                dData = dd;
        }

        // -------------------------------------------------------------
        public void displayLink() // display this link
        {
                System.out.print(dData + " ");
        }
} // end class Link
// //////////////////////////////////////////////////////////////

class SortedList {
        private Link first; // ref to first item

        // -------------------------------------------------------------

        public SortedList() // constructor
        {
                first = null;
        }

        // -------------------------------------------------------------
        public boolean isEmpty() // true if no links
        {
                return (first == null);
        }

        // -------------------------------------------------------------
        public void insert(long key) // insert, in order
        {
                Link newLink = new Link(key); // make new link
                Link previous = null; // start at first
                Link current = first;
                // until end of list,
                while (current != null && key > current.dData) { // or key > current,
                        previous = current;
                        current = current.next; // go to next item
                }
                if (previous == null) // at beginning of list
                        first = newLink; // first --> newLink
                else
                        // not at beginning
                        previous.next = newLink; // old prev --> newLink
                newLink.next = current; // newLink --> old currnt
        } // end insert()
                // -------------------------------------------------------------

        public Link remove() // return & delete first link
        { // (assumes non-empty list)
                Link temp = first; // save first
                first = first.next; // delete first
                return temp; // return value
        }

        // -------------------------------------------------------------
        public void displayList() {
                Link current = first; // start at beginning of list
                while (current != null) // until end of list,
                {
                        current.displayLink(); // print data
                        current = current.next; // move to next link
                }
        }
} // end class SortedList
// //////////////////////////////////////////////////////////////

class PriorityQ {
        private SortedList theList;

        // -------------------------------------------------------------
        public PriorityQ() // constructor
        {
                theList = new SortedList();
        }

        // -------------------------------------------------------------
        public void insert(long item) // insert item
        {
                // simply inserting item to sorted list
                theList.insert(item);
        }

        // -------------------------------------------------------------
        public long remove() // remove minimum item
        {
                // removing first link from sorted list (link with smallest value), and
                // returning data of removed link. assuming queue is not empty.
                return theList.remove().dData;
        }

        // -------------------------------------------------------------
        public boolean isEmpty() // true if queue is empty
        {
                // returns true if underlying sortedlist is empty
                return theList.isEmpty();
        }
        // -------------------------------------------------------------
} // end class PriorityQ
// //////////////////////////////////////////////////////////////

class PriorityQApp {
        public static void main(String[] args) {
                PriorityQ thePQ = new PriorityQ();

                thePQ.insert(30);
                thePQ.insert(50);
                thePQ.insert(10);
                thePQ.insert(40);
                thePQ.insert(20);

                while (!thePQ.isEmpty()) {
                        long item = thePQ.remove();
                        System.out.print(item + " "); // 10, 20, 30, 40, 50
                } // end while
                System.out.println("");
        } // end main()
                // -------------------------------------------------------------
} // end class PriorityQApp

/*OUTPUT*/

10 20 30 40 50 

Related Solutions

C++ Program 2–Implement a Priority queue using a SORTED list. Use Quicksort after adding a new...
C++ Program 2–Implement a Priority queue using a SORTED list. Use Quicksort after adding a new node.Example of quick sort below. Adopt to your program. #include <iostream> voidquickSort(inta[], intfirst, intlast); intpivot(inta[], intfirst, intlast); voidswap(int& a, int& b); voidswapNoTemp(int& a, int& b); voidprint(intarray[], constint& N); usingnamespacestd; intmain() { inttest[] = { 7, -13, 1, 3, 10, 5, 2, 4 }; intN = sizeof(test)/sizeof(int); cout << "Size of test array :"<< N << endl; cout << "Before sorting : "<< endl; print(test,...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being...
Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being served at the Department of Motor Vehicles. Start with 100 customers in a List. In a loop, generate a priority for each customer (1-5) In a second loop, add the users to a priority queue Print the List and the Priority Queue
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node will have a 4 digit job number and a priority (A, B, etc) with A highest priority In the driver Create a priority queue object Add 3 jobs of 'B' priority Add 4 jobs of 'D' priority Add 2 jobs of highest priority Print the queue
Write a C program to implement the priority queue with the operations insert and extractmax. Sample...
Write a C program to implement the priority queue with the operations insert and extractmax. Sample : ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 2 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 4 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 6 enter any key to go to main menu...
Create a program that creates a sorted list from a data file. The program will prompt...
Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name ,last...
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements....
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill in...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is the right-most node. The remove (de-queue) operation returns the node with the highest priority (key). If displayForward() displays List (first-->last) : 10 30 40 55 remove() would return the node with key 55. Demonstrate by inserting keys at random, displayForward(), call remove then displayForward() again. You will then attach a modified DoublyLinkedList.java (to contain the new priorityInsert(long key) and priorityRemove() methods). Use the provided...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT