Question

Language C++ (Please include a short description & Screenshot of output) Implement a Priority...

Language C++ (Please include a short description & Screenshot of output)

Implement a Priority queue using a SORTED list. Use Quick sort after adding a new node.

Example of quick sort below. Adopt to your program the code below.

#include <iostream>

void quickSort(int a[ ], int first, int last);

int pivot(int a[], int first, int last);

void swap(int& a, int& b);

void swapNoTemp(int& a, int& b);

void print(int array[], const int& N);

using namespace std;

int main() {

int test[] = { 7, -13, 1, 3, 10, 5, 2, 4 };

int N = sizeof(test)/sizeof(int);

cout << "Size of test array :" << N << endl;

cout << "Before sorting : " << endl;

print(test, N);

quickSort(test, 0, N-1);

cout << endl << endl << "After sorting : " << endl; print(test, N);

return 0; }

/**

* Quicksort.

* @param a - The array to be sorted.

* @param first - The start of the sequence to be sorted.

* @param last - The end of the sequence to be sorted.

*/

void quickSort( int a[], int first, int last ) {

int pivotElement;

if(first < last) {

pivotElement = pivot(a, first, last);

quickSort(a, first, pivotElement-1);

quickSort(a, pivotElement+1, last); }

}

/**

* Find and return the index of pivot element.

* @param a - The array.

* @param first - The start of the sequence.

* @param last - The end of the sequence.

* @return - the pivot element

*/

int pivot(int a[], int first, int last) {

int p = first;

int pivotElement = a[first];

for(int i = first+1 ; i++) {

/* If you want to sort the list in the other order, change "" */ if(a[i] <= pivotElement) {

p++; swap(a[i], a[p]);

}

}

swap(a[p], a[first]);

return p; }

/**

* Swap the parameters.

* @param a - The first parameter.

* @param b - The second parameter.

*/

void swap(int& a, int& b) {

int temp = a;

a = b;

b = temp; }

/**

* Swap the parameters without a temp variable.

* Warning! Prone to overflow/underflow.

* @param a - The first parameter.

* @param b - The second parameter.

*/

void swapNoTemp(int& a, int& b) {

a -= b;

b += a;// b gets the original value of a

a = (b - a);// a gets the original value of b }

/**

* Print an array.

* @param a - The array.

* @param N - The size of the array.

*/ void print(int a[], const int& N) {

for(int i = 0 ; i < N ; i++)

cout << "array[" << i << "] = " << a[i] << endl; }

--------------------------------------------------------------------------------------------------------------------------

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

Dequeue

Enqueue Miko, 7

Enqueue Vlady, 8

Enqueue Frank, 9

Enqueue Anny, 3

Dequeue

Enqueue Xi, 2

Enqueue Wali, 2

Enqueue xChe, 6

Enqueue xVerra, 8

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

0 0
Add a comment Improve this question Transcribed image text
Answer #1

At first, a class called node is created whose data members are name and priority. The object of this class denotes each element of the priority queue. Then a class queue is created which contains the node array of objects. Here the functions like enqueue(), dequeue() and show are defined. After adding each element in the queue, the queue is sorted using quicksort algorithm in ascending order of priority. Finally, the main() contains a menu-driven list of choices and an object of queue class is created to call enqueue(), dequeue() and show() when needed/

Source code:

#include<iostream>
using namespace std;
class node
{
   public:
       string name;
       int priority;
       void input(string n,int p)
       {
           name=n;
           priority=p;
       }
       void display()
       {
           cout<<"\n"<<name<<"\t"<<priority<<endl;
       }
};
void swap(node *a,node *b)
{
   node t;
   t=*a;
   *a=*b;
   *b=t;
}
class queue
{
   public:
       node *a=NULL;
       int size,f,r;
       queue(int n)
       {
           size=n;
           a=new node[size];
           f=r=-1;
       }
       void enqueue()
       {
           string nm;
           int p;
           cout<<"\nEnter name: ";
           cin>>nm;
           cout<<"\nEnter priority: ";
           cin>>p;
           node x;
           x.input(nm,p);
           if(r==size-1)
               cout<<"\nQueue is full"<<endl;
           else if((f==-1)&&(r==-1))
           {
               f=r=0;
               a[r]=x;
           }
           else
           {
               r++;
               a[r]=x;
           }
           quicksort(f,r);
       }
       void quicksort(int l,int r)
       {
           int loc;
           if(l<r)
           {
               loc=pivot(l,r);
               quicksort(l,loc-1);
               quicksort(loc+1,r);
           }
       }
       int pivot(int l,int r)
       {
           int p=a[r].priority;
           node t;
           int i=l-1;
           for(int j=l;j<=r-1;j++)
           {
               if(a[j].priority<p)
               {
                   i++;
                   swap(&a[i],&a[j]);
               }
           }
           swap(&a[i+1],&a[r]);
           return (i+1);
       }
       void dequeue()
       {
           if((f==-1)||(f>r))
               cout<<"\nQueue is empty"<<endl;
           else
           {
               cout<<"Deleted element: ";
               a[f++].display();
           }
       }
       void show()
       {
           if((f==-1)||(f>r))
           {
               cout<<"\nQueue is empty"<<endl;
               return;
           }
           for(int i=f;i<=r;i++)
           {
               a[i].display();
           }
           cout<<endl;
       }
};
int main()
{
   int ch,size;
   cout<<"\nEnter size of queue: ";
   cin>>size;
   queue ob(size);
   while(1)
   {
       cout<<"\nPriority queue operations";
       cout<<"\n1.Enqueue";
       cout<<"\n2.Dequeue";
       cout<<"\n3.Display";
       cout<<"\n4.Exit";
       cout<<"\nEnter your choice: ";
       cin>>ch;
       switch(ch)
       {
           case 1:
           ob.enqueue();
           break;
          
           case 2:
           ob.dequeue();
           break;
          
           case 3:
           ob.show();
           break;
          
           case 4:
           exit(0);
          
           default:
           cout<<"\nInvalid choice.\n";
       }
   }
   return 0;
}

output:

CAUsersUSERDesktop testiqueue.exe nter size of queue 20 riority queue operations Enqueue Dequeue Display Exit nter your choic

CAUsers\USER\Desktopltestiqueue.exe Priority queue operations 1. Enqueue 2. Dequeue 3.Display 4.Exit nter your choice: 3 red

CAUsers \USER\Desktopltestiqueue.exe Priority queue operations 1. Enqueue 2. Dequeue Display nter your choice: 1 nter name Mo

CAUsersUSERDesktopltestiqueue.exe riority queue operations Enqueue Dequeue Display Exit nter your choice: 1 nter name:Ulady n

İ CAUsersNUSERNDesktoptestiqueue.exe Priority queue operations 1. Enqueue 2. Dequeue Display 4.Exit nter your choice: 3 ое ое

CAUsers USERDesktopltestiqueue.exe Priority queue operations 1.Enqueue 2. Dequeue 3.Display 4. Exit Enter your choice: 1 Ente

Priority queue operations 1. Enqueue 2. Dequeue Display Exit Enter your choice: 2 Deleted element Wali riority queue operatio

Add a comment
Know the answer?
Add Answer to:
Language C++ (Please include a short description & Screenshot of output) Implement a Priority...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT