Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Queue.cpp //--------------------------------------------------------------- // File: Queue.cpp //--------------------------------------------------------------- #include #include using namespace std; #define MAX_SIZE 50 // Define maximum length of the queue class Queue { private:

image text in transcribed
Queue.cpp
//---------------------------------------------------------------
// File: Queue.cpp
//---------------------------------------------------------------
#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
cout
}
cout
}
T-Mobile LTE 12:50 AM Q nsu.blackboard.comm You may ansume the classes Stack and Queue hold integers 1 of Stack will contain the usual functions void Pop Let Top D vold displaystack dlass Gueue will cotais the usual functions Enter an iten in the qaee bool Enqueue (int nun int Dequeue bool Espeyo Remove an item feom the queue Retuen true it queue is enpty Retuen crue sE queue is full Example ol a problem Suppase we have a queue Q, containing some integers If we want so reverse the integers in the queve, we can use a stack. Like this Stack 5 while 1O.Epty01 S-push 10 Deque while 0S.Empty01 Enqaee (S Topt1) S.Popt 1. Given a Queue (50 points) Enquee 200 randomly generated inbegers ranging 200-3000, inclusively) into the Q Modify the queue by change all of its even values to the number 2. Otherwise, the final values in the queue will be the same and in the same oeder but with al even values changed 2. Given a Quee uQueue (50 points) Enqu 200 randomly generated integers (ranging 0-120, indlusivelyl into the nuspueue b Search the numQueue for the element that has walue 60 or greater and return the count of 60 inthe queue. . Print out all other mumbers (except60') in the original order 3. Given a Stack Write code to reverse all the elements in S. Algorithm: Create a temporary queue transfer all of the stack's elements inte the queue, transfer all of the queue's items back into the stack Note: The algorithm for this question is very similar to the example given above Sa (Bonus 20 points)-only get bonus you finish the first 2 programs

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Brad Bulger, Jay Greenspan, David Wall

2nd Edition

0764549634, 9780764549632

More Books

Students also viewed these Databases questions