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 queuevoid enqueue(String item) // insert a new string onto queueString dequeue() //remove and return the string LEAST recently addedboolean isEmpty() //is the queue emptyint 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.