Question
c++ #include #include using namespace std; #define MAX_SIZE 50 // Define maximum length of the queue class Queue { private: int head, tail; // Index
c++
#include #include using namespace std;
#define MAX_SIZE 50 // Define maximum length of the queue
class Queue { private: int head, tail; // Index to top of the queue int theQueue[MAX_SIZE]; // The queue
public: Queue(); // Class constructor bool Enqueue(int num); // Enter an item in the queue int Dequeue(); // Remove an item from the queue bool isEmpty(); // Return true if queue is empty bool isFull(); // Return true if queue is full void displayQueue(); }; Queue::Queue() { head = tail = -1; } bool Queue::Enqueue(int num) { // Check to see if the Queue is full if(isFull()) return false;
// Increment tail index tail++; // Add the item to the Queue theQueue[tail % MAX_SIZE] = num; return true; } int Queue::Dequeue() { int num;
// Check for empty Queue if(isEmpty()) return '\0'; // Return null character if queue is empty else { head++; num = theQueue[head % MAX_SIZE]; // Get character to return return num; // Return popped character } }
bool Queue::isEmpty() { return (head == tail); } bool Queue::isFull() { // Queue is full if tail has wrapped around // to location of the head. See note in // Enqueue() function. return ((tail - MAX_SIZE) == head); }
void Queue::displayQueue(){ for (int i = head+1; i <= tail; i++) { cout << theQueue[i] << " ... "; } cout << endl << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started