C++ #include <iostream> #include <string.h> using namespace std; #define MAX_SIZ
ID: 3700252 • Letter: C
Question
C++
#include <iostream>
#include <string.h>
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 ''; // 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;
}
Explanation / Answer
//Que.h as provided , no change
#pragma once
#include <iostream>
#include <string.h>
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();
};
------------------------------------------------------------------------
//Que.cpp as provided , no change
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 ''; // 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;
}
--------------------------------------------------------------------------
//main program,,
#include"Que.h"
int main()
{
Queue q;
//enque some numbers onto queue
for (int i = 0; i < 10; i++)
{
q.Enqueue(i + 2);
}
//display elements of queue
cout << "Contents of the queue are: ";
q.displayQueue();
//dequeue some elements of queue and display
q.Dequeue();
q.Dequeue();
cout << "Contents of the queue after two deque operation: ";
q.displayQueue();
}
-------------------------------------------------------------------------------------------
//output
Contents of the queue are: 2 ... 3 ... 4 ... 5 ... 6 ... 7 ... 8 ... 9 ... 10 ... 11 ...
Contents of the queue after two deque operation: 4 ... 5 ... 6 ... 7 ... 8 ... 9 ... 10 ... 11 ...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.