Your problems are usually recognized as a complete or empty circular queue problem . Citation:
To solve this confusion, there are a number of solutions:
- Always keep one slot open.
- Use the fill amount to distinguish two cases.
- Use the extra mirror bit to distinguish between two cases.
- Use the read and write amount to get a fill counter.
- Use absolute indexes.
- Record the last operation.
Numbers 1, 2 and 4 you directly address your question. They consume a certain amount of memory, except for the array and indexes / start / end indexes (or front / back, as you call them). Other solutions also consume memory.
# 3 - use the mirror bit
Adds only one additional logical value or enumeration, essentially isEmpty or isFull . The idea, logic, and proof of this approach are more complex .
# 5 - absoulte indices
Indexes increase when an operation is performed and never decreases. They are essentially counters of the number of operations of each type performed. These are other disadvantages .
# 6 - record the last operation
Essentially the same as # 3, but different semantics.
In any case, all of these links relate to the wikipedia article. I highly recommend it. There may be other approaches, but it would be difficult to improve one of the 6 approaches outlined in your article. They all have disadvantages as well as advantages, so think carefully about the intended use case before proceeding with implementation.
source share