

Plus, linked lists already have a front and a back built-in, making the "never access the middle" honor code easy to enforce because…it's hard to reach the middle elements. Unlike arrays, linked lists let you spread the elements throughout your computer's memory, making them more flexible for adding and removing elements.

If you decide to implement a queue using a linked list, you'll want to use a singly-linked list, where each node connects to the next node in the list.

Just like in real life, if you make a promise that you're going to do something, you better be ready to follow through. If you promise in your code that you're using a queue, you can't just jump around anywhere you want in the array, because other coders won't be able to understand your work. Using an array means you could look anywhere in the queue if you really wanted to, but…don't.
QUEUE FIFO UPDATE
It's a simpler and faster solution to just update your front pointer to reference each time and be done with it. Shifting the whole array could take way too much time and energy, depending on how large your queue is, since you'll have to move every single element any time you want to take something out. You can either shift the whole array down by one slot ( queue moves to queue, queue moves to queue, and so on) or just use a pointer to keep track of where the front of the queue now is. Because queue is assumed to be the front, you'll need to do something to fill the space when you take the front element out, making that first value null. Queues as arrays get tricky the minute you try to remove an element. If the final position for an element is at index n, then you'll add a new element to the index n + 1. If you create a queue using an array, the front is assumed to be at the index 0 ( queue_name), at least to start.

You can create a queue using either a linked list or an array, but most people go with arrays because queues are just ordered like that. You can't just jump around anywhere you want in the queue, otherwise…you shouldn't be using a queue. Just like with stacks, you can only see one end of the queue, but this time that end's the beginning. When you take something out, you're dequeuing it, which takes out the first element. Whenever you add something to a queue, you're enqueuing it, which adds it to the back of the line. This is in an idealized world where every line has those stretchy tape poles that keep people from trying to jump into the line wherever they want. Queues are an abstract data type (ADT) where the first element in is also the first element out, making them First In, First Out (FIFO) and Last In, Last Out (LILO) structures. If you're waiting in line (on line?), you're actually standing in a queue. Cutting in line can be annoying, but as long as you don't shop for cured meats on Black Friday, you probably won't see any fist fights. Maybe people have been talking about creating a FIFA-style shoot-out match in the can aisle to resolve any claims of cutting, but that sounds like a lot ofĪs fun as it sounds to accidentally topple over a stack of tomato soup cans and start a stock avalanche in aisle three, waiting in line's probably better for both you and the store. Sure, nobody likes it, but at the same time it feels much more fair than other forms of deciding who goes first at the deli counter line. Anyone who doesn't live in a town of two to three people has waited in a line before.
