Question

In: Computer Science

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 the blank, etc ( Like prior program done with LISTS )

Solutions

Expert Solution

#include <iostream>
#include <string>
using namespace std;

class Node {
   public:
      string name;
      int priority;
};


//Implement prioroty queue class
class PriorityQueue {
  
    private:
    Node array[10];
    int capacity;
    int front;
    int rear;
      
    public:
        PriorityQueue() {
            front = 0;
            rear = 0;
            capacity = 10;
        }

//adding elements

        void enQueue(string name, int priority) {
            if (isFull()) {
                cout << "Error : Queue Full" <<endl;
                return;
            }
            Node node;
            node.name = name;
            node.priority = priority;
            array[rear] = node;
            rear = (rear+1)%capacity;
            displayElements();
        }

//popping elements
        void deQueue() {
            if (isEmpty()) {
                cout << "Queue is Empty" << endl;
                return;
            }
          
            if (rear > front) {
                int highPriorityIndex = findMaxPriorityNode(front, rear-1);
                shiftElements(highPriorityIndex, rear-1);
            }
            else {
                int indexOne = findMaxPriorityNode(front, capacity-1);
                int indexTwo = findMaxPriorityNode(0, rear-1);
              
                if (array[indexOne].priority >= array[indexTwo].priority) {
                    shiftElements(front, capacity-1);
                    return;
                }
                shiftElements(0, rear);
            }
            rear = (rear-1)%capacity;
            displayElements();
        }
      
        bool isEmpty() {
            if (front == rear)
                return true;
            return false;  
        }
      
        bool isFull() {
            if ((rear+1)%capacity == front)
                return true;
            return false;
        }
      
//showing elements
        void displayElements() {
            if (isEmpty()) {
                cout << "Queue is Empty" << endl;
                return;
            }
          
            cout << "The contents of th Queue are : ";
            if (rear > front) {
                printElems(front, rear-1);
            }
            else {
                printElems(front, capacity-1);
                printElems(0, rear-1);
            }
            cout << endl;
        }
      
       
        /**
         *
         * Private helper methods
         */
        private:
        int findMaxPriorityNode(int start, int end) {
            int highPriority = -1;
            int highPriorityIndex = -1;
            for (int i=start; i<=end; i++) {
                Node node = array[i];
                if (node.priority > highPriority) {
                    highPriorityIndex = i;
                    highPriority = node.priority;
                }
            }
            return highPriorityIndex;
        }
      
//shifting elements
        void shiftElements(int start, int end) {
            for (int i=start; i<end; i++) {
                array[i] = array[i+1];
            }
        }
// printing elements    
        void printElems(int start, int end) {
            for (int i=start; i<=end; i++) {
                cout << "["<<array[i].name <<"," << array[i].priority << "] ";
            }
        }
};

int main()
{
PriorityQueue p;
p.enQueue("Rog", 3);
p.enQueue("Sam", 1);
p.deQueue();

return 0;
}

Output screenshot:


Related Solutions

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...
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue...
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue object and try various operations below. Queue myQueue = new Queue(); myQueue.enqueue(“CPS 123”); myQueue.enqueue(“CPS 223”); myQueue.enqueue(“CPS 323”); myQueue.dequeue(); myQueue.enqueue(“CPS 113”); myQueue.enqueue(“CPS 153”); string course = myQueue.front(); // course should be CPS 223 size = myQueue.size(); // size should be 4 // output course and size
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.
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the kth smallest item in an array. Use the first item as the pivot. Compare sets of results using a static call counter. Reset counter before running another search. Create a Test drive to exhaustively test the program. // Assume all values in S are unique. kSmall(int [] S, int k): int (value of k-smallest element) pivot = arbitrary element from S:  let’s use the first...
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...
Implement a stack in C++ using an array, not an array list. Make your stack size...
Implement a stack in C++ using an array, not an array list. Make your stack size 5 when you test it, but do not hardcode this! You should be able to change the size for testing purposes with the change of one variable. DO NOT use Stack class defined in C++ Implement the following methods in your stack class. stack() creates an empty stacks, stack s is new and empty. push(item) adds a new item to the stack s, stacks...
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
Implement the minimum priority queue UnsortedMPQ (using vector) that is a child class of the provided...
Implement the minimum priority queue UnsortedMPQ (using vector) that is a child class of the provided MPQ class. The functions from MPQ that are virtual function (remove min(), is empty(), min(), and insert()) must be implemented in the child classes. The functions remove min() and min() should throw an exception if the minimum priority queue is empty. For the UnsortedMPQ class, you will use a vector to implement the minimum priority queue functions. The insert() function should be O(1) and...
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT