Queue.cpp //--------------------------------------------------------------- // F
ID: 3700070 • Letter: Q
Question
Queue.cpp
//--------------------------------------------------------------- // File: Queue.cpp //--------------------------------------------------------------- #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; }
//--------------------------------------------------------------- // File: Queue.cpp //--------------------------------------------------------------- #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; }
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
Explanation / Answer
Hi,
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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.