|
| 1 | +--- |
| 2 | +tags: |
| 3 | +- datatypes/collections |
| 4 | +--- |
| 5 | + |
| 6 | +# The Queue collections |
| 7 | + |
| 8 | +## Introduction |
| 9 | + |
| 10 | +### Abstract Data Type |
| 11 | + |
| 12 | +Described [abstractly](./lectures/data/intro#abstract-data-types), [a queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) is |
| 13 | + |
| 14 | +- a finite collection of elements, |
| 15 | +- in a particular order, |
| 16 | +- that may contain the same element multiple times. |
| 17 | + |
| 18 | +The fact that it may contain the same element multiple times makes it different from a set, the fact that it is ordered makes it different from a [multiset](https://en.wikipedia.org/wiki/Multiset). |
| 19 | + |
| 20 | +Generally, it has operations to… |
| 21 | + |
| 22 | +- … create an empty queue, |
| 23 | +- … test for emptiness, |
| 24 | +- … obtain the value of the element at "the end" of the queue ("peek"), |
| 25 | +- … add an element at "the beginning" of the queue ("enqueue"), |
| 26 | +- … remove an element at "the end" of the queue ("dequeue"). |
| 27 | + |
| 28 | +The fact that only the "last element" can be removed and that elements can only be added "at the front" is the main difference with the list abstract data type. |
| 29 | +Exactly like a people waiting in line for a service, queues implement a " first-in-first-out" (FIFO) principle. |
| 30 | + |
| 31 | +### Difference with array |
| 32 | + |
| 33 | +Like lists, queues serve a similar purpose than arrays, but with a few notable differences: |
| 34 | + |
| 35 | +- Queues do not need to have a number of elements fixed ahead of time, |
| 36 | +- Queues automatically expand when elements are added, |
| 37 | +- Queues automatically shrink when elements are removed. |
| 38 | + |
| 39 | +### Difference with lists |
| 40 | + |
| 41 | +However, queues have differences with lists: |
| 42 | + |
| 43 | +- Only the "last" element's value can be read (accessed), |
| 44 | +- Adding an element can only be done "on the left side", that is, at the beginning of the queue. |
| 45 | + |
| 46 | +## Possible Implementation |
| 47 | + |
| 48 | +### Using Cells |
| 49 | + |
| 50 | +One could implement queue by adapting our `CList` class, making sure that the read, remove and insert operations are limited to the "valid" elements. |
| 51 | + |
| 52 | +### Using Arrays |
| 53 | + |
| 54 | +One could implement queue by using arrays, adding elements "at the end" of the array and removing them "from the beginning". |
| 55 | +This implementation has one major drawback, however: in this implementation, enqueue operation is $O(c)$, but dequeue is $O(n)$, since one need to "shift" all the elements by one after the first one has been removed. |
| 56 | + |
| 57 | +An alternative is to use a *circular array*, where the beginning and the end of the array are "glued together". This requires to manipulate three `int` variables: |
| 58 | + |
| 59 | +- `front`, pointing to the first element, |
| 60 | +- `end`, pointing to the last element, |
| 61 | +- and `size`, the number of elements in the queue. |
| 62 | + |
| 63 | +An attribute `mArray` then contain the elements in the queue, *not necessarily in the "right" order*, since as the queue is dequeued, its "front" moves. |
| 64 | + |
| 65 | +```{download="./code/projects/CQueue.zip"} |
| 66 | +!include code/projects/CQueue/CQueue/CQueue.cs |
| 67 | +``` |
| 68 | + |
| 69 | + |
| 70 | +An implementation using `capacity` instead of `end` is [also possible](https://www.geeksforgeeks.org/dsa/introduction-to-circular-queue/#), but it requires to use the modulo operation (`%`) to compute the `end`, using |
| 71 | + |
| 72 | +``` |
| 73 | +end = (front + size) % capacity; |
| 74 | +``` |
| 75 | + |
| 76 | +Our implementation can recover the capacity of the queue by using: |
| 77 | + |
| 78 | +``` |
| 79 | +capacity = mArray.Length - size; |
| 80 | +``` |
| 81 | +. |
0 commit comments