Skip to content

Commit 5ed5b75

Browse files
committed
Added explanation on Queue computed size.
1 parent 229f66d commit 5ed5b75

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

source/lectures/data/queue.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,12 @@ public int CSize
104104
```
105105

106106
but the exception would be thrown: can you figure out in which case(s) the "computed size" would be incorrect?
107+
<details>
108+
<summary>Solution</summary>
109+
The implementation is *almost* correct, the problem is that `front == end` can be true for two reasons:
110+
111+
#. The queue is empty, no element have been added yet (hence, size should be 0),
112+
#. The queue is full, no element can be added (hence, size should be `mArray.Length`).
113+
114+
Since `IsFull`, `IsEmpty` and `Capacity` all uses `size`, we have no way of telling if we are in the first or second situation. The rest of the implementation is correct, but `size` must be an attribute to be able to differenciate those two cases (note that we could also have added a `bool` to distinguish between "full" and "empty", but it's more convenient to have a `size` attribute available at all time).
115+
</details>

0 commit comments

Comments
 (0)