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...
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,...
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...
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.
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...
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...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions:...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions: • myAdd ( ): add an integer d to the array; return 0 if the operation is successful; return a negative number otherwise. • search ( ): given an integer d, if d is found in the array, return the index of the cell containing d. Return a negative number otherwise (e.g., d is not found in the array). • myRemove ( ): Step...
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)
Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a...
Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a class called Node: Have a Name and Priority.Data set - 10 is the highest priority, 1 is lowest priority. Enqueue and dequeue in the following order. Function  Name, Priority Enqueue  Joe, 3 Enqueue  Fred,1 Enqueue Tuyet,9 Enqueue  Jose, 6 Dequeue Enqueue  Jing, 2 Enqueue  Xi, 5 Enqueue  Moe, 3 DequeueEnqueue  Miko, 7 Enqueue Vlady, 8 Enqueue Frank, 9 Enqueue  Anny, 3 DequeueEnqueue  Xi, 2 Enqueue  Wali, 2 Enqueue  xChe, 6 Enqueue  xVerra, 8 Dequeue Dequeue Dequeue Dequeue...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT