Skip to content

Queues

Let’s consider a queue implementation by looking at a corresponding API and the operations that a queue would include.

QueueOfStrings() // Create an empty queue
void enqueue(String item) // insert a new string onto queue
String dequeue() //remove and return the string LEAST recently added
boolean isEmpty() //is the queue empty
int size() //number of strings on the queue

Just like stacks we have similar operations of adding and removing however the naming conventions and semantics are important to note. Similar to queues in real life adding an item to a queue puts it as the newest addition to the object and removing an item from a queue would be removing the item that was least recently added. To put this into a real life example.

If we had a queue to towards a night club door new patrons arriving to go in would enqueue and the patrons who were at the front of the line waiting the longest would dequeue after going in.