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

LAB 18.4 Dynamic Queue Write a program that will create a dynamic queue of any d

ID: 3717208 • Letter: L

Question

LAB 18.4 Dynamic Queue

Write a program that will create a dynamic queue of any data type. Demonstrate your program with a driver program.

class Dynque

{

     private:

           struct QueueNode

           {

                float value;

                QueueNode *next;

           };

    

           QueueNode *front;

           QueueNode *rear;

           int numItems;

     public:

     Dynque();

     ~Dynque();

     void enqueue(float);

     void dequeue(float &);

     bool isEmpty();

     bool isFull();

     void clear();

};

Sample Run:

EClUsersluserDesktopProject1Debug Project1.exe Enqueuing 5 items... he values in the queue were: Press any key to continue . . .

Explanation / Answer

#include <iostream>

using namespace std;


class Dynque
{
     private:
           struct QueueNode
           {
                float value;
                QueueNode *next;
           };
  
           QueueNode *front;
           QueueNode *rear;
           int numItems;
     public:
      Dynque();
      ~Dynque();
      void enqueue(float);
      void dequeue(float &);
      bool isEmpty();
      bool isFull();
      void clear();
};

Dynque::Dynque() {
   front = NULL;
   rear = NULL;
   numItems = 0;
}

Dynque::~Dynque() {
   clear();
}

void Dynque::enqueue(float num) {
   QueueNode *n = new QueueNode;
   n->value = num;
   n->next = rear;
   if(rear == NULL) {
       rear = n;
       front = n;
   } else {
       rear = n;
   }
}

void Dynque::dequeue(float &num) {
   num = front->value;
   front = front->next;
   if(front == NULL) {
       rear = NULL;
   }
}

bool Dynque::isEmpty() {
   return front == NULL;
}

bool Dynque::isFull() {
   return false;
}

void Dynque::clear() {
  
}


int main() {
   cout << "Enqueuing 5 items..." << endl;
   Dynque q;
   q.enqueue(0);
   q.enqueue(1.1);
   q.enqueue(2.2);
   q.enqueue(3.3);
   q.enqueue(4.4);
   cout << "The values in the queue were:";
   float num;
   while(!q.isEmpty()) {
       q.dequeue(num);
       cout << num << endl;
   }
   return 0;
}