A queue is a data structure that follows a first-in first-out (FIFO) principle. Similar to the stack you can add elements to the queue (enqueue), remove elements from the queue (dequeue), and check if the queue is full.
π― Problem Statement
Write a queue data structure that stores βcuteβ objects. A cute object is an object that conforms to the Cutie interface / abstract base class (as outlined in the Dev Notes). You can use an array βunder the hoodβ, meaning that your queue will have a maximum size.
β Acceptance Criteria
The queue data structure should be an object.
The queue should use an array list βunder the hoodβ to store Cutie objects. You do not need to create a queue that resizes itself with new inputs.
I should be able to enqueue(...) and dequeue(...) a Cutie object to the queue data structure.
If I try to enqueue an object onto the queue while it is full, I should see a message telling me the queue is full. The queue should remain unchanged.
I should be able to get the current size of the queue via a size(...) method (i.e. how many elements are in the queue).
π Dev Notes
You cannot use the built-in queue in this solution.
You do not have to implement the Cutie interface / abstract base class from scratch. I have provided it here:
C++
class Cutie { private: string description; int cuteness_rating public: Cutie(string description, int cuteness_rating) { this->description = description; this->cuteness_rating = cuteness_rating; } virtual string get_description() = 0; virtual int get_cuteness_rating() = 0;}
Java
interface Cutie { public String description(); // All cuties need to > have a description of what makes them cute. public Integer cutenessRating(); // All cuties get a cuteness rating out of ten.}
π₯οΈ Example Output
Suppose you wrote a queue data structure called QueueTees. You then created Puppy, Kitty, and PygmyMarmoset which all conform to that interface / abstract base class. Your driver program might look something like this:
C++
int main() { // Create a bunch of objects that implement the Cutie base class Puppy puppy; Kitty kitty; PygmyMarmoset marmoset; // Create a queue data structure QueueTees queue; // The size of the queue should equal zero since there are no objects in it cout << queue.size(); // Add the cuties to the queue queue.enqueue(puppy); queue.enqueue(kitty); queue.enqueue(marmoset); // The size of the queue should equal three since there are three objects in it cout << queue.size(); // The first dequeue should return the puppy queue.dequeue(); // The second dequeue should return the kitty queue.dequeue(); // The third dequeue should return the pygmy marmoset queue.dequeue();}
Java
// Create a bunch of objects that conform to the Cutie interfacePuppy puppy = new Puppy();Kitty kitty = new Kitty();PygmyMarmoset marmoset = new PygmyMarmoset();// Create a queue data structureQueueTees queue = new QueueTees();// The size of the queue should equal zero since there are no objects in itSystem.out.println(queue.size());// Add the cuties to the queuequeue.enqueue(puppy);queue.enqueue(kitty);queue.enqueue(marmoset);// The size of the queue should equal three since there are three objects in itSystem.out.println(queue.size());// The first dequeue should return the puppyqueue.dequeue();// The second dequeue should return the kittyqueue.dequeue();// The third dequeue should return the pygmy marmosetqueue.dequeue();
π Thought Provoking Questions
How might you create a queue with no theoretical maximum size?
Right now, your queue is restricted to store Cutie objects only. How might you change your queue class to store a variety of different types of objects?
πΌ Add-Ons For the Portfolio
(One Credit) Clear Method
Create a method that clears all elements in the queue.
(Three Credits) Generic Type for Queue
Update your queue class to allow the user to specify what type of object will be stored in the queue. You can use a class template to specify the type of object. The behavior of your queue should not change otherwise.