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

write program that does the following // queue struct struct Queue{ // \"static\

ID: 672644 • Letter: W

Question

write program that does the following

// queue struct

struct Queue{

// "static" because there is a single MAX_SIZE constant for the

// entire Queue struct, not a separate one for each

// Queue object. (Without the "static", we couldn't use

// this for the array size.)

// (Incidently, the syntax in the following statement would not

// work for a double. We're allowed to have and initialize

// a static const value of type double, but we have to write

// it differently.)

static const unsigned MAX_SIZE = 3;

string ...[ MAX_SIZE]; // you decide the array name

... // other field variables as needed, you decide which ones

};

void initialize( Queue & q )

{

// set the data members of q so that q represents a queue with 0

// items

}

unsigned size( const Queue & q )

{

// return the # of elements in the (abstract) queue represented by q

// return value will always be in [0, Queue::MAX_SIZE].

}

void add( Queue & q, const string & item )

{

// if q is full (already has MAX_SIZE items), exit program

// Otherwise, add a copy of item to the end of the queue.

// This should be done without moving any elements already in the

// queue; use the "wrap-around" trick so that the queue wraps

// around from the end of the array back to the beginning.

// (add should not (exit program) unless the abstract queue being represented

   // already has MAX_SIZE items (not fewer).)

}

string remove( Queue & q )

{

// if q is empty (0 items), exit program

// Otherwise, remove the item at the front of the queue,

// and return its value.

// (Don't move the elements already in the queue.)

}

string first( const Queue & q )

{

// if q is empty, exit program

// Otherwise, return a copy of the item at the front of the queue

// (without removing it).

}

string last( const Queue & q )

{

// if q is empty, exit program

// Otherwise, return a copy of the item at the rear of the queue

// (without removing it).

}

void show( const Queue & q )

{

// Output the elements in the queue,

// starting at the front of the queue

}

Explanation / Answer

// queue struct
struct Queue{
// "static" because there is a single MAX_SIZE constant for the
// entire Queue struct, not a separate one for each
// Queue object. (Without the "static", we couldn't use
// this for the array size.)
// (Incidently, the syntax in the following statement would not
// work for a double. We're allowed to have and initialize
// a static const value of type double, but we have to write
// it differently.)
static const unsigned MAX_SIZE = 3;
string storage[ MAX_SIZE]; // you decide the array name
// other field variables as needed, you decide which ones
int rear;
int front;
int count;
};
void initialize( Queue & q )
{
// set the data members of q so that q represents a queue with 0
// items
q->front = -1;
q->rear = -1;
q->count = 0;
}
unsigned size( const Queue & q )
{
// return the # of elements in the (abstract) queue represented by q
// return value will always be in [0, Queue::MAX_SIZE].
return q->count;
}
void add( Queue & q, const string & item )
{
// if q is full (already has MAX_SIZE items), exit program
// Otherwise, add a copy of item to the end of the queue.
// This should be done without moving any elements already in the
// queue; use the "wrap-around" trick so that the queue wraps
// around from the end of the array back to the beginning.
// (add should not (exit program) unless the abstract queue being represented
// already has MAX_SIZE items (not fewer).)
if(q->count == MAX_SIZE)
exit(1);
else
{
q->rear ++;
q->rear = q->rear % MAX_SIZE;
q->storage[q->rear] = item;
q->count ++;
}
}
string remove( Queue & q )
{
// if q is empty (0 items), exit program
// Otherwise, remove the item at the front of the queue,
// and return its value.
// (Don't move the elements already in the queue.)
if(q->count == 0)
exit(1);
else
{
string item;
q->front ++;
q->front = q->front % MAX_SIZE;
item = q->storage[q->front];
q->count --;
return item;
}
}
string first( const Queue & q )
{
// if q is empty, exit program
// Otherwise, return a copy of the item at the front of the queue
// (without removing it).
if(q->count == 0)
exit(1);
else
return q->storage[q->front];
}
string last( const Queue & q )
{
// if q is empty, exit program
// Otherwise, return a copy of the item at the rear of the queue
// (without removing it).
if(q->count == 0)
exit(1);
else
return q->storage[q->rear];
}
void show( const Queue & q )
{
// Output the elements in the queue,
// starting at the front of the queue
int temp = q->front;
int c = q->count;
while(c != 0)
{
temp ++;
temp = temp % MAX_SIZE;
cout<<q->storage[temp]<<" ";
c --;
}
}