Please help with writing this in C++, everything on chegg has numerous errors an
ID: 3581024 • Letter: P
Question
Please help with writing this in C++, everything on chegg has numerous errors and does not compile.
Solve the starvation problem using any simple, suitable technique
1) Write a queue class using linked list to implement the queue. Do not us the link list from STL. The queue class should be a template class and must implement enqueue and dequeue operations. It should also have a getQLength() function that returns the number of elements in the queue.
2) Write a PriQueue class that uses the queue class. The PriQueue is also a template class. It should take a parameter "levels" as input to specify the number of priority levels. The constructor should create a vector "pq" of size "levels" to create the various priority levels. pq should be a private variable. It should overload the enqueue operation from Queue class: void enqueue(T obj, int level). It allows you to specify the priority level of the object "obj" so that you can place the object in the right queue. By default, the enqueue operation puts the object in queue 0 (lowest priority). Therefore, the function void enqueue(T obj) should place the object in queue pq[0]. The dequeue operation needs to check the queues in the order of priority. The higher levels need to be served before the lower ones. As long as there is something in the higher queue, the lower queue cannot be served. (Note that there are other ways to implement priority but we are sticking to this rule).
3) The PriQueue should implement a function void increaseLevel() that adds another priority level by inserting another level in the vector "pq". For example:
Queue q;
pq.pushback(q).
4) Test your implementation using a driver program. You should try enqueuing and dequeuing at various levels and submit the ouput along wth the code.
Explanation / Answer
1)
#include<iostream>
#include<cstdlib>
using namespace std;
struct node{
int info;
struct node *next;
};
class Queue{
private:
node *rear;
node *front;
public:
Queue();
void enqueue();
void dequeue();
void display();
};
Queue::Queue(){
rear = NULL;
front = NULL;
}
void Queue::enqueue(){
int data;
node *temp = new node;
cout<<"Enter the data to enqueue: ";
cin>>data;
temp->info = data;
temp->next = NULL;
if(front == NULL){
front = temp;
}else{
rear->next = temp;
}
rear = temp;
}
void Queue::dequeue(){
node *temp = new node;
if(front == NULL){
cout<<" Queue is Emtpty ";
}else{
temp = front;
front = front->next;
cout<<"The data Dequeued is "<<temp->info;
delete temp;
}
}
void Queue::display(){
node *p = new node;
p = front;
if(front == NULL){
cout<<" Nothing to Display ";
}else{
while(p!=NULL){
cout<<endl<<p->info;
p = p->next;
}
}
}
int main(){
Queue queue;
int choice;
while(true){
cout<<" 1.Enqueue 2. Dequeue 3. Display 4.Quit";
cout<<" Enter your choice: ";
cin>>choice;
switch(choice){
case 1:
queue.enqueue();
break;
case 2:
queue.dequeue();
break;
case 3:
queue.display();
break;
case 4:
exit(0);
break;
default:
cout<<" Invalid Input. Try again! ";
break;
}
}
return 0;
}
2)
#include<iostream>
#include<cstdlib>
#define default_value 16
using namespace std;
template< class T > class Queue
{
public:
Queue(int = default_value);//default constructor
~Queue()//destructor
{delete [] values;}
bool enQueue( T );
T deQueue();
bool isEmpty();
bool isFull();
private:
int size;
T *values;
int front;
int back;
};
template< class T > Queue<T>::Queue(int x):
size(x),//ctor
values(new T[size]),
front(0),
back(0)
{ /*empty*/ }
template< class T > bool Queue<T>::isFull()
{
if((back + 1) % size == front )
return 1;
else
return 0;
}
template< class T > bool Queue<T>::enQueue(T x)
{
bool b = 0;
if(!Queue<T>::isFull())
{
values[back] = x;
back = (back + 1) % size;
b = 1;
}
return b;
}
template< class T > bool Queue<T>::isEmpty()
{
if( back == front )//is empty
return 1;
else
return 0; //is not empty
}
template< class T > T Queue<T>::deQueue()
{
T val = -1;
if(!Queue<T>::isEmpty())
{
val = values[front];
front = ( front + 1 ) % size;
}
else
{
cerr << "Queue is Empty : ";
}
return val;
}
int main()
{
Queue <float> qu1(32);
float x = 1.1;
int i = 1; //declare software counter i
cout << " EnQueued values into qu1: ";
for( ; i < 10 ; i++) //start enter 10 elements into Qu1 object
{
if(qu1.enQueue(i*x))
cout << endl << i*x;
else
cout << " Queue (qu1) is full: ";
}
cout << " DeQueued values from qu1 : ";
for(int j = 1 ; j < 10 ; j++)
cout << qu1.deQueue() << endl;
cout << endl << endl;
return 0;
}