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

Implement a Circular Queue using the functions named below. #ifndef QUEUE_H #def

ID: 3804772 • Letter: I

Question

Implement a Circular Queue using the functions named below.

#ifndef QUEUE_H
#define QUEUE_H

#include
using namespace std;

//
// Exception classes
//
class QueueEmpty { /* Empty error class */ }; // Exception class for empty queue condition         

class QueueFull { /* Empty error class */ }; // Exception class for full queue condition

class QueueInvalidPeek { /* Empty error class */ }; // Exception class for invalid queue peek condition

//
// Queue Node Structure
//
struct Node                         // Linked circular queue node structure
{
int         data;    // Field for storing data in the queue node
Node*     nextPtr;   // Points to successor node (node following current node)
};


//
// Queue class declaration
//
class Queue                // Linked circular queue
{
private:
    Node*       rearPtr;            // Points to rear of queue
int   count;    // Number of values stored in queue

public:
/********** Start of functions you must implement for Queue **************/
// Implement the following nine public functions in the file named queue.cpp

Queue();
// Queue()
// Initializes all private variables to indicate an empty queue

~Queue();
//~Queue()
// Deallocates all queue nodes

void MakeEmpty();
// MakeEmpty()
// Deallocates all queue nodes and returns queue to empty ready-to-use state

    void Enqueue(int n);
// Enqueue()
// Adds value n to rear of queue and increments count.
// If queue is already full, throws QueueFull exception

void Dequeue();
// Dequeue()
// Removes front value from queue and decrements count.
// If queue is empty, throws QueueEmpty exception

int Front() const;
// Front()
// Returns integer from front of queue
// If queue is empty, throws QueueEmpty exception
    // DOES NOT MODIFY THE QUEUE

int Rear() const;
// Rear()
// Returns integer from rear of queue
// If queue is empty, throws QueueEmpty exception
    // DOES NOT MODIFY THE QUEUE

int Peek(int n) const;
// Peek()
// Returns integer n positions from front of queue
    // If queue is empty, throws QueueEmpty
// If position n does not exist, throws QueueInvalidPeek
    // DOES NOT MODIFY THE QUEUE

bool IsFull() const;
// IsFull()
// Returns true if queue is full. Returns false otherwise. DOES NOT MODIFY THE QUEUE

bool IsEmpty() const;
// IsEmpty()
// Returns true if queue is empty. Returns false otherwise. DOES NOT MODIFY THE QUEUE

int Size() const;
// Size()
// Returns number of items stored in queue. DOES NOT MODIFY THE QUEUE

/*********** End of functions you must implement for Queue ***************/

void PrintQ() const
// PrintQ() -- DO NOT MODIFY OR RELOCATE THIS FUNCTION
// Prints contents of queue front to rear without modifying its contents
{
  cout << "Front { ";
        if (rearPtr != NULL)
        {
            Node* tempPtr = rearPtr->nextPtr;
            int n = 0;
         while (n < count)
         {
          cout << tempPtr->data << ' ';
       n++;
             tempPtr = tempPtr->nextPtr;
         }
        }
  cout << "} Rear";
} // End Print()

};

#endif

Explanation / Answer

queue.cpp

#include "queue.h"


Queue::Queue()
{
   rearPtr = nullptr;
}

bool Queue::IsFull() const
{
   if (count >= 5)
   {
       return true;
   }
   return false;
}
// IsFull()
// Returns true if queue is full. Returns false otherwise. DOES NOT MODIFY THE QUEUE

bool Queue::IsEmpty() const
{
   if (rearPtr == nullptr)
   {
       return true;
   }
   return false;
}
// IsEmpty()
// Returns true if queue is empty. Returns false otherwise. DOES NOT MODIFY THE QUEUE

void Queue::Enqueue(int n)
{
   if (!IsFull())
   {
       Node *temp = new Node;
       temp->data = n;
       temp->nextPtr = rearPtr;
       rearPtr = temp;
       count++;
   }
   else
   {
       throw QueueFull();
   }
}
// Adds value n to rear of queue and increments count.
// If queue is already full, throws QueueFull exception

void Queue::Dequeue()
{
   if (!IsEmpty())
   {
       Node *temp = rearPtr;
       Node *tempdel = new Node;
       for (int i = 1; i < count; i++)
       {
           temp = temp->nextPtr;
       }
       tempdel = temp->nextPtr;
       temp->nextPtr = nullptr;
       delete tempdel;
   }
   else
   {
       throw QueueEmpty();
   }
}
// Removes front value from queue and decrements count.
// If queue is empty, throws QueueEmpty exception

int Queue::Front() const
{
   Node *temp = rearPtr;
   while(temp->nextPtr!=nullptr)
   {
       temp=temp->nextPtr
   }
   return temp->data;
}
// Returns integer from front of queue
// If queue is empty, throws QueueEmpty exception
// DOES NOT MODIFY THE QUEUE

int Queue::Rear() const
{
   return rearPtr->data;
}
// Returns integer from rear of queue
// If queue is empty, throws QueueEmpty exception
// DOES NOT MODIFY THE QUEUE

int Queue::Peek(int n) const
{
   if (Size() > n)
   {
       Node *temp = rearPtr;
       for (int i = 0; i < n; i++)
       {
           temp = temp->next;
       }
       return temp->data;
   }
   else
   {
       throw QueueInvalidPeek();
   }
}
// Returns integer n positions from front of queue
// If queue is empty, throws QueueEmpty
// If position n does not exist, throws QueueInvalidPeek
// DOES NOT MODIFY THE QUEUE

int Queue::Size() const
{
   return count;
}
// Returns number of items stored in queue. DOES NOT MODIFY THE QUEUE

void Queue::MakeEmpty()
{
   while (!IsEmpty())
   {
       Dequeue();
   }
}
// Deallocates all queue nodes and returns queue to empty ready-to-use state

Queue::~Queue()
{
   MakeEmpty();
}


queue.h

//
// queue.h
//
// Queue class is a circular linked list implementation of the queue abstract data type
//
// DO NOT MODIFY OR SUBMIT THIS FILE
//

#ifndef QUEUE_H
#define QUEUE_H

#include <iostream>
using namespace std;

//
// Exception classes
//
class QueueEmpty { /* Empty error class */ }; // Exception class for empty queue condition        

class QueueFull { /* Empty error class */ }; // Exception class for full queue condition

class QueueInvalidPeek { /* Empty error class */ }; // Exception class for invalid queue peek condition

//
// Queue Node Structure
//
struct Node                         // Linked circular queue node structure
{
   int         data;               // Field for storing data in the queue node
   Node*        nextPtr;           // Points to successor node (node following current node)
};


//
// Queue class declaration
//
class Queue                            // Linked circular queue
{
private:
    Node*       rearPtr;            // Points to rear of queue
   int           count;               // Number of values stored in queue
  
public:
   /********** Start of functions you must implement for Queue **************/
   // Implement the following nine public functions in the file named queue.cpp
  
   Queue();
   // Queue()
   // Initializes all private variables to indicate an empty queue
  
   ~Queue();
   //~Queue()
   // Deallocates all queue nodes
  
   void MakeEmpty();
   // MakeEmpty()
   // Deallocates all queue nodes and returns queue to empty ready-to-use state
  
    void Enqueue(int n);
   // Enqueue()
   // Adds value n to rear of queue and increments count.
   // If queue is already full, throws QueueFull exception

   void Dequeue();
   // Dequeue()
   // Removes front value from queue and decrements count.
   // If queue is empty, throws QueueEmpty exception

   int Front() const;
   // Front()
   // Returns integer from front of queue
   // If queue is empty, throws QueueEmpty exception
    // DOES NOT MODIFY THE QUEUE

   int Rear() const;
   // Rear()
   // Returns integer from rear of queue
   // If queue is empty, throws QueueEmpty exception
    // DOES NOT MODIFY THE QUEUE

   int Peek(int n) const;
   // Peek()
   // Returns integer n positions from front of queue
   // If queue is empty, throws QueueEmpty
   // If position n does not exist, throws QueueInvalidPeek
   // DOES NOT MODIFY THE QUEUE
  
   bool IsFull() const;
   // IsFull()
   // Returns true if queue is full. Returns false otherwise. DOES NOT MODIFY THE QUEUE
  
   bool IsEmpty() const;
   // IsEmpty()
   // Returns true if queue is empty. Returns false otherwise. DOES NOT MODIFY THE QUEUE
  
   int Size() const;
   // Size()
   // Returns number of items stored in queue. DOES NOT MODIFY THE QUEUE

   /*********** End of functions you must implement for Queue ***************/
  
   void PrintQ() const
   // PrintQ() -- DO NOT MODIFY OR RELOCATE THIS FUNCTION
   // Prints contents of queue front to rear without modifying its contents
   {
       cout << "Front { ";
        if (rearPtr != NULL)
        {
            Node* tempPtr = rearPtr->nextPtr;
            int n = 0;
             while (n < count)
             {
                 cout << tempPtr->data << ' ';
                n++;
                 tempPtr = tempPtr->nextPtr;
             }
        }
       cout << "} Rear";
   } // End Print()
  
};

#endif

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