Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives: USING STACK Question: Accompanying this question is a skeleton of a

ID: 3578540 • Letter: O

Question

Objectives: USING STACK

Question:

Accompanying this question is a skeleton of a program that partially defines a Queue class, as well as a main function that uses an instance of the Queue. Finish the Queue class by completing the method Queue::show so that the output of the program matches the output below. No modifications to the Queue class nor the main function are required, such changes are prohibited.

Code so far:

Example Output:

Enqueueing 5 people

fred barney wilma steve april

Dequeueing 5 people

Enqueueing 3 people

jack jill claire

Dequeueing 1 person

jill claire

Enqueueing 2 people

jill claire brad janet

Explanation / Answer

#include <iostream>
//#include

using namespace std;

#define CAP 5
class Queue {
public:
        void enq(string name);
        string deq();
        int count();
        bool isFull();
        bool isEmpty();
        void show();
private:
        string line[CAP];
        int size = 0, front = 0, back = 0;
};

bool Queue::isFull() {
        return (size == CAP);
}

bool Queue::isEmpty() {
        return (size == 0);
}

void Queue::enq(string name) {
        if (isFull()) {
                /* Queue is full */
                return;
        }
        line[back] = name;
        back = (back + 1) % CAP;
        size++;
}

string Queue::deq() {
        if (isEmpty()) {
                /* Queue is empty */
                return "";
        }
        string s = line[front];
        front = (front + 1) % CAP;
        size--;
        return s;
}

void Queue::show() {
        /*
         * Complete me
         */
         Queue q2 = Queue();
         int sz = size;
         int n = 0;
         while(n<sz){
                 string elem = deq();
                 cout<<elem<<" ";
                 enq(elem);
                 n++;
         }
         cout<<endl;
}

int main(void) {
        Queue q;
        cout << "Enqueueing 5 people" << endl;
        q.enq("fred");
        q.enq("barney");
        q.enq("wilma");
        q.enq("steve");
        q.enq("april");
        q.show();

        cout << "Dequeueing 5 people" << endl;
        q.deq(); q.deq(); q.deq(); q.deq();     q.deq();
        q.show();

        cout << "Enqueueing 3 people" << endl;
        q.enq("jack");
        q.enq("jill");
        q.enq("claire");
        q.show();

        cout << "Dequeueing 1 person" << endl;
        q.deq();
        q.show();

        cout << "Enqueueing 2 people" << endl;
        q.enq("brad");
        q.enq("janet");
        q.show();

        return 0;
}