Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Task For this assignment you are to create a C++ class that will implement a pri

ID: 3789929 • Letter: T

Question

Task

For this assignment you are to create a C++ class that will implement a priority queue. For your queue low numbers will take highest priority. Your class should implement the following functions

-          Void enq(int)

-          Void deq()

-          Void front()

-          Bool isEmpty()

-          Void printq()

Your program will take in a command file called “cmd.txt” which will instruct your program what operations to run

File Format

<command> <0 or 1 arguments>

Commands

-          1            enq

-          2            deq

-          3            front

-          4            isEmpty

-          5            printq

Example File

1 5

5

1 4

5

1 3

5

1 2

5

1 1

5

1 30

5

3

2

5

2

5

3

Expectations

You should not use any already implemented code such as a library for your linked list

Your code should be well formatted with proper spacing and proper naming

Your code should have well named variables. No a’s b’s or c’s as names unless it is for something like a loop counter

Your code MUST have the same output formatting you see below

Your file MUST be named a3.cpp

My example file does not cover all possible cases your program may be tested against. It is up to you to think about and consider any edge cases that may come up and cause your program to crash.

Example Output

Back K-- Front 1 Front Back 2 1 Front Back 3 2 1 K-- Front Back 4 3 2 1 Front Front 1 De q 1. Front Back 2 4 3 2 K- Deq Back 4 3 K-- Front Front 3 Queue is not empty Back 4 3 Front Back 4 3 Front

Explanation / Answer

Code for a3.cpp

#include<iostream>

#include <fstream>
using namespace std;

typedef struct list
{
int value;
struct list* next;
}List;

class priorityList
{
private:
List* begin;
   List* rear;
   int sizeofList;

public:

priorityList(){
sizeofList = 0;
begin = rear = NULL;
}
     
void enq(int newval){

List* iterator = NULL, *before = NULL;
List* temp = new List;
temp->value = newval;
temp->next = NULL;
   sizeofList++;

if(begin == NULL){
begin = rear = temp;
return;
}
else{
iterator = begin;
while( (iterator != NULL) && ((iterator->value) < newval)){
before =iterator;
iterator = iterator->next;
}

if(begin == iterator){
temp->next = begin;
begin = temp;
}
else if( iterator == NULL){
rear->next = temp;
rear = temp;
}
else{
before->next = temp;
temp->next = iterator;
}
}
return;
   }

void deq(){
List* temp = begin;
if((begin == NULL) && (rear == NULL)){
cout<<"Deq:"<<" ";
return;
}
else if((begin == rear) && (begin != NULL)){
begin = rear = NULL;
}
else{
begin = begin->next;
temp->next = NULL;
}
cout<<"Deq: "<<temp->value<<" ";
delete temp;
}

void front(){
if(begin != NULL)
cout<< "Front: " << begin->value <<" ";
else
cout<< "Front: " <<" ";
}

bool isEmpty(){
if( (begin == NULL) && (rear == NULL))
return true;
else
return false;
}

void printq(){
int val[sizeofList], i=-1;
List* iterator = begin;
cout<<"Back --> ";
if((begin != NULL) && (rear != NULL)){
while(iterator != NULL){
val[++i] = iterator->value;
iterator = iterator->next;
}
while(i >=0){
cout<< val[i] <<" ";
i--;
}
}
cout<<"<-- Front ";
}


};

int main()
{
priorityList valuelist ;
int cmd = -1, arg = -1;
bool flag;
  
/*Please provide the full path of the command file */
ifstream inputfile("./cmd.txt");

while( inputfile >> cmd){

switch(cmd){
case 1:
inputfile >> arg;
valuelist.enq(arg);
break;
case 2:
valuelist.deq();
break;
case 3:
valuelist.front();
break;
case 4:
flag = valuelist.isEmpty();
if(flag == true)
cout<<"Queue is empty" <<" ";
else
cout<<"Queue is not empty" <<" ";
break;
case 5:
valuelist.printq();
break;
default:
cout << "Invalid input : " << cmd <<" ";
break;
}
}
  
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote