Question: A queue can hold anything, and later in the course we'll see how to make it more flexible with C++ templates. But for now, your

A queue can hold anything, and later in the course we'll see how to make it more flexible with C++ templates.

But for now, your queue is going to hold coordinates. A coordinate is an integer x,y pair referring to a position on a grid.

We'll all use this simple public-data class for our coordinates:

class coord { public: int x, y; coord() { x = 0; y = 0; } coord(int _x, int _y) { x = _x; y = _y; } }; 

Your queue class must support the following operations (public methods):

empty: return true if the queue has 0 items in it

size: return the number of items in the queue

push: add a new item to the back of the queue

pop: remove and return the item at the front of the queue

Internally, you must use a private linked list to implement this functionality. That means you'll also need a node class that holds data of type coord.

Start by stubbing out your classes. coord, node and your queue class, the last one with the public methods specified above. Then use this test code to ensure that your queue works. Remember!

First, look over the whole thing to make sure you understand what you're trying to accomplish.

Then, work on the tests a few at a time.

#include  
#include  
#include  
int main() { ListQueue q; // empty case assert(q.size() == 0); assert(q.empty() == true); // add some items coord c(1, 2); q.push(c); // note: reusing c works here because we're storing copies of c in the queue c.x = 3; c.y = 4; q.push(c); // note: here is a simpler way, omitting the name of the temporary coord // note: this is called an "anonymous" object, which is used and discarded q.push(coord(5,6)); // test! assert(q.size() == 3); assert(q.empty() == false); coord check = q.pop(); assert(q.size() == 2); assert(check.x == 1); assert(check.y == 2); check = q.pop(); assert(q.size() == 1); assert(check.x == 3); assert(check.y == 4); check = q.pop(); assert(q.size() == 1); assert(check.x == 5); assert(check.y == 6); assert(q.size() == 0); assert(q.empty() == true); return 0; } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!