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 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. You will then attach priorityInsert(long key) and priorityRemove() methods). AND Use the provided PQDoublyLinkedTest.java to test your code. BOTH CODES SHOULD WORK TOGETHER, YOU JUST HAVE TO ADD priorityInsert(int). PLEASE PROVIDE...
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), and a driver...
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...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT