9) Queues¶
FIFO, which stands for First-In-First-Out. This is another way of writing First Come First Serve. As the name queue implies, you can imagine it to be like a line at a ticket counter, and the first person being addressed in the queue will be the person that entered the queue first, and once that person is addressed, they’ll be the first to leave. Then the second person that entered the queue will be addressed, then the third, and so on until the queue is empty.Create the Queue
Destroy the Queue
Find the size of the Queue
Check if the Queue is empty
Check if the Queue is full (Array based implementation only)
Insert an item into the Queue (also called an Enqueue or Push operation)
Remove an item from the Queue (also called a Dequeue or Pop operation)
Return the item at the front of the Queue (also called a Peek operation)
Clear the Queue
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Start:
- - - - -Enqueue(3):
3 - - - -3 is AddedEnqueue(4):
3 4 - - -4 is AddedEnqueue(5):
3 4 5 - -5 is AddedDequeue():
4 5 - - -3 is ReleasedEnqueue(2):
4 5 2 - -2 is AddedEnqueue(8):
4 5 2 8 -8 is AddedDequeue():
5 2 8 - -4 is ReleasedDequeue():
2 8 - - -5 is ReleasedDequeue():
8 - - - -2 is ReleasedDequeue():
- - - - -8 is Released
enqueue it starts at the first location and does a linear search until it finds a free space. Although functional, it makes the complexity of enqueue O(n), which is extremely slow. On top of that, the dequeue operation also has a complexity of O(n) because of the shifting of elements when a value is dequeued.Start:
- - - - -, Front == -1, Rear == -1Enqueue(3):
3 - - - -, Front == 0, Rear == 0, 3 is AddedEnqueue(4):
3 4 - - -, Front == 0, Rear == 1, 4 is AddedEnqueue(5):
3 4 5 - -, Front == 0, Rear == 2, 5 is AddedDequeue():
- 4 5 - -, Front == 1, Rear == 2, 3 is ReleasedEnqueue(2):
- 4 5 2 -, Front == 1, Rear == 3, 2 is AddedEnqueue(8):
- 4 5 2 8, Front == 1, Rear == 4, 8 is AddedDequeue():
- - 5 2 8, Front == 2, Rear == 4, 4 is ReleasedDequeue():
- - - 2 8, Front == 3, Rear == 4, 5 is ReleasedDequeue():
- - - - 8, Front == 4, Rear == 4, 2 is ReleasedDequeue():
- - - - -, Front == -1, Rear == -1, 8 is ReleasedEnqueue(1):
1 - - - -, Front == 0, Rear == 0, 1 is AddedEnqueue(2):
1 2 - - -, Front == 0, Rear == 1, 2 is AddedEnqueue(3):
1 2 3 - -, Front == 0, Rear == 2, 3 is AddedEnqueue(4):
1 2 3 4 -, Front == 0, Rear == 3, 4 is AddedEnqueue(5):
1 2 3 4 5, Front == 0, Rear == 4, 5 is Added
enqueue and dequeue down to O(1), as the Array immediately knows which place to add a value to, and return a value from.front == 4, rear == 4, programmer uses enqueue(). There are free values in the queue, at indexes 0, 1, 2, 3, but if you didn’t make your queue circular then you might be returning an error statement instead. Making your queue circular means moving the rear back to index 0 by keeping track of free spaces. Doing this will increase the amount of IF statements you have to write but effectively lets you have an infinite amount of enqueue and dequeue operations as long as there aren’t more than n items in the queue at once.n items in the queue at once, since it’s an array. In space saving measures this is fine behaviour but if we want a general purpose Queue, we want to be able to grow it as much as possible. That’s where the Linked List implementation comes in. It’s effectively the same logic, and it keeps the O(1) complexity of the enqueue` and dequeue operations (by keeping pointers for Front and Rear, similar to how you remembered indexes in the Array version), but with the added advantage that it can grow infinitely. Here’s how it would look (Front and Rear are pointers, when I’ve written Front == 3 it means Front is pointing to a Node in a Linked List, and the data in that node is 3. Front itself is NOT equal to 3!):Start:
NULL, Front ==nullptr, Rear ==nullptrEnqueue(3):
3, Front == 3, Rear == 3Enqueue(4):
3 -> 4, Front == 3, Rear == 4Enqueue(5):
3 -> 4 -> 5, Front == 3, Rear == 5Dequeue():
4 -> 5, Front == 4, Rear == 5, 3 is ReleasedEnqueue(2):
4 -> 5 -> 2, Front == 4, Rear == 2Enqueue(8):
4 -> 5 -> 2 -> 8, Front == 4, Rear == 8Dequeue():
5 -> 2 -> 8, Front == 5, Rear == 8, 4 is ReleasedDequeue():
2 -> 8, Front == 2, Rear == 8, 5 is ReleasedDequeue():
8, Front == 8, Rear == 8, 2 is ReleasedDequeue():
NULL, Front ==nullptr, Rear ==nullptr, 8 is Released
Being able to write an answer using a set of rules or requirements
Being able to find an answer using a set of rules or requirements
T* arrint sizeint frontint rear
Node<T>* frontNode<T>* rear
williamwoha. It’s the easiest way to contact me (without revealing my real life identity or phone number) which is why I use it. Good luck!