I know that y’all have been waiting on this queue for too long. So let’s get right down to the code of the matter. Unlike Stacks, Queues are all about FIFO, think about FIFA, except not really. When you are in a queue you would hope that the first person in line would get served first. With this in mind let’s build this out!
We created a variable that would hold all our items together. Then we created a function that would append new items. The item being appended goes to the end of the queue. Furthermore, when we dequeue an item we need to check to see if we even have an item to begin with. Do not be the presumptuous programmer who assumes anything, check yourself and check your optionals! So once we’ve guarded our items in our queue, then we also check to see if the first element actually exists, if it does GREAT let’s return it! However, if it does not you should return nil because you don’t have anything in your queue! Finally, the peek method should be apparent you want to see who is in your queue and once again the item or person you would most care about is the first person.
Let’s look at an actual example of this:
So here we have the Lannisters. When we first peek inside we have no Lannister’s in our queue. However, after adding in three of the, you can see that we are returning the Lannister that we are also predicting to die this season. Queue’s and cersei’s death should be of no surprise to anyone. When trying to write the code for a Queue try to be empathetic with users and what I say by this is think of a time you were in line or serving someone in line…how would you go about helping them? That mentality should help you be able to write the code for a Queue and remember its core concepts!