c++ #include #include using namespace std; #define MAX_SIZE 50 // Define maximum
ID: 3700399 • Letter: C
Question
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 ''; // 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
Below are the required files. I have created 3 files for each exercise. Also I have created Stack.h and Stack.cpp as these classes are not given. I have seperated Queue.cpp into Queue.h and Queue.cpp, so Queue.cpp now will only have method definitions. Run all three main files and verify the Outputs. Let me know if any questions.
Queue.h
Queue.cpp
Stack.h
Stack.cpp
lab09LessThan60.cpp
lab09ModifyQueue.cpp
lab09ReverseStack.cpp
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.