Question

In: Computer Science

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

Solutions

Expert Solution

Solution:

PROGRAM 1:

CODE:

#include <iostream>

using namespace std;

const int MAX=10;

/*Create struct node with maximum limit value*/
struct node
{
int data;
string nameVal;
int priorityVal;
}d[MAX];

/*Class for priority queue with attributes of name and priority*/
class pqueue
{
int front,rear;
public:
pqueue()
{
front=rear=-1;
}
void enqueue(struct node d1);
struct node dequeue();
void display();
};
/*Enqueue - insert/add the value to the queue*/
void pqueue :: enqueue(struct node d1)
{
if(rear==MAX-1) //-- check queue is full or not before adding value to queue
cout<<"Priority Queue is Full";
else
{
rear++;
d[rear]=d1;
if(front==-1)
front=0;
struct node temp;
for(int i=front;i<=rear;i++)
for(int j=i+1;j<=rear;j++)
{
if(d[i].priorityVal > d[j].priorityVal) //-- insert values based on it's priority order
{
temp=d[i];
d[i]=d[j];
d[j]=temp;
}
else
{
if(d[i].priorityVal==d[j].priorityVal)
{
temp=d[i];
d[i]=d[j];
d[j]=temp;
}
}
}
}
}
struct node pqueue :: dequeue()
{
struct node d1;
if(front==-1) //-- check queue is empty before deleting elements from it.
cout<<"Priority Queue is Empty";
else
{
d1=d[front];
if(front==rear)
front=rear=-1;
else
front++;
}
return d1;
}
/*Display the list of elements in the queue*/
void pqueue :: display()
{
if(front==-1)
cout<<"Priority Queue is Empty";
else
{
for(int i=front;i<=rear;i++)
{
cout<<"Name ="<<d[i].nameVal<<endl;
cout<<"Priority="<<d[i].priorityVal<<endl;
}
}
}
int main()
{
pqueue p1;
struct node d1;
char op;
do
{
int ch;
cout<<"1.Enqueue " << endl << "2.Dequeue" << endl << "3.Display" << endl << "4.Exit" << endl;
cout<<"Enter your Choice(1..4) ?";
cin>>ch;
switch(ch)
{
case 1 : cout<<"Enter Name ?"<<endl;
cin>>d1.nameVal;
cout<<"Enter Priority?"<<endl;
cin>>d1.priorityVal;
p1.enqueue(d1);
break;
case 2 : d1=p1.dequeue();
cout << "Dequeued data"<<endl;
cout<<"Name = "<<d1.nameVal<<endl;
cout<<"Priority = "<<d1.priorityVal<<endl;
break;
case 3 : p1.display();
break;
}
cout<<"Do You Want to Continue <Y/N> ?";
cin>>op;
}while(op=='Y' || op=='y');
}

Sample output:

#please consider my effort and give me a like..thank u...


Related Solutions

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...
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.
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
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...
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...
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...
Given the following program with fill in the blanks, implement the PQ class which is priority...
Given the following program with fill in the blanks, implement the PQ class which is priority Queue MAX heap; the higher the number, the higher the prority of that number. None of the public methods has been implemented. You will need to write other methods to help implement some of the methods. For example you will need to implement bubbleUp to help with the insert method. Methods to implement: insert -> insert one element into the PQ and maintain heap...
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...
// 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT