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

look at the class defined in pqueue.h, the main.cpp and cmd.txt file, create the

ID: 3881814 • Letter: L

Question

look at the class defined in pqueue.h, the main.cpp and cmd.txt file, create the corresponding implementation in pqueue.cpp

pqueue.h

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

#ifndef PQ_H
#define PQ_H

// include this library to use NULL, otherwise use nullptr instead
#include <cstddef>
#include <iostream>

/* Struct which will be the building block of our list */
struct node{
int val;
node* next;
};

/* Linked list class definition */
class PQUEUE{
public:
PQUEUE();
void enq(int);
bool deq();
    bool isEmpty();
    node* getFront();
void printq();
private:
node* front;
    node* back;
};

#endif

main.cpp

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

include <fstream>
#include "pqueue.h"
using namespace std;

int main(int argc, char **argv)
{
PQUEUE myQueue;
ifstream input;
int cmd, argument, ret;
    node* frontNode = NULL;

    if(argc < 2)
    {
        cout << "useage: ./a3.out <cmd file> ";
        return -1;
    }
input.open(argv[1]);

    // while there is something to read from the file, read
while (input >> cmd)
{
        // switch on the command we read from the file
  switch (cmd)
  {
        // if the cmd requires a parameter, read it from the file and call the
        // associated function
  case 1:
   input >> argument;
   myQueue.enq(argument);
            cout << "Added " << argument << " to the queue ";
   break;
  case 2:
   if(myQueue.deq())
            {
                cout << "Removed front of queue ";
            }
            else
            {
                cout << "Nothing to remove from the queue ";
            }
   break;
  case 3:
            frontNode = myQueue.getFront();
            if(NULL == frontNode)
            {
                cout << "Nothing is in the queue ";
            }
            else
            {
                cout << "Front of the queue contains " << frontNode->val << endl;
            }
            break;
        case 4:
            if(myQueue.isEmpty())
            {
                cout << "Queue is empty ";
            }
            else
            {
                cout << "Queue has data ";
            }
            break;
        case 5:
            myQueue.printq();
            break;
        }
}
input.close();

return 0;
}

cmd.txt

---------

1 9
5
3
1 3
5
3
1 5
5
3
1 1
5
3
1 4
5
3
2
5
3
2
5
3
2
5
3
4
2
5
2
5
2
5
4

Explanation / Answer

// pqueue.h

#ifndef PQ_H

#define PQ_H

// include this library to use NULL, otherwise use nullptr instead

#include <cstddef>

#include <iostream>

/* Struct which will be the building block of our list */

struct node {

       int val;

       node* next;

};

/* Linked list class definition */

class PQUEUE {

public:

       PQUEUE();

       void enq(int);

       bool deq();

       bool isEmpty();

       node* getFront();

       void printq();

private:

       node* front;

       node* back;

};

#endif

#include "pqueue.h"

using namespace std;

PQUEUE::PQUEUE()

{

       front = NULL;

       back = NULL;

}

bool PQUEUE::isEmpty()

{

       if (front == NULL )

              return true;

       else

              return false;

}

void PQUEUE::enq(int item)

{

       node *cur = front, *newNode;

       newNode = new node;

       newNode->val = item;

       newNode->next = NULL;

       if (isEmpty())

       {

              front = newNode;

              back = front;

       }

       else

       {

              back->next = newNode;

              back = newNode;

       }

}

bool PQUEUE::deq()

{

       node *tmp;

       if (front != NULL)

       {

              tmp = front;

              front = front->next;

              delete tmp;

              return true;

       }

       back = NULL;

       return false;

}

node *PQUEUE::getFront()

{

       return front;

}

void PQUEUE::printq()

{

       node *cur = front;

       cout << "Queue content: ";

       while (cur!=NULL)

       {

              cout << cur->val << " " ;

              cur = cur->next;

       }

       cout << endl;

}

//main.cpp,no change still sending

#include <fstream>

#include "pqueue.h"

using namespace std;

int main(int argc, char **argv)

{

       PQUEUE myQueue;

       ifstream input;

       int cmd, argument, ret;

       node* frontNode = NULL;

       if (argc < 2)

       {

              cout << "useage: ./a3.out <cmd file> ";

              return -1;

       }

       input.open(argv[1]);

      

       // while there is something to read from the file, read

       while (input >> cmd)

       {

              // switch on the command we read from the file

              switch (cmd)

              {

                      // if the cmd requires a parameter, read it from the file and call the

                      // associated function

              case 1:

                      input >> argument;

                      myQueue.enq(argument);

                      cout << "Added " << argument << " to the queue ";

                      break;

              case 2:

                      if (myQueue.deq())

                      {

                             cout << "Removed front of queue ";

                      }

                      else

                      {

                             cout << "Nothing to remove from the queue ";

                      }

                      break;

              case 3:

                      frontNode = myQueue.getFront();

                      if (NULL == frontNode)

                      {

                             cout << "Nothing is in the queue ";

                      }

                      else

                      {

                             cout << "Front of the queue contains " << frontNode->val << endl;

                      }

                      break;

              case 4:

                      if (myQueue.isEmpty())

                      {

                             cout << "Queue is empty ";

                      }

                      else

                      {

                             cout << "Queue has data ";

                      }

                      break;

              case 5:

                      myQueue.printq();

                      break;

              }

       }

       input.close();

       return 0;

}

//output with given cmd.txt file

Added 3 to the queue

Queue content: 9 3

Front of the queue contains 9

Added 5 to the queue

Queue content: 9 3 5

Front of the queue contains 9

Added 1 to the queue

Queue content: 9 3 5 1

Front of the queue contains 9

Added 4 to the queue

Queue content: 9 3 5 1 4

Front of the queue contains 9

Removed front of queue

Queue content: 3 5 1 4

Front of the queue contains 3

Removed front of queue

Queue content: 5 1 4

Front of the queue contains 5

Removed front of queue

Queue content: 1 4

Front of the queue contains 1

Queue has data

Removed front of queue

Queue content: 4

Removed front of queue

Queue content:

Nothing to remove from the queue

Queue content:

Queue is empty