Implement a ring-buffer. Sigh...this was tremendously frustrating. I understand a ring buffer’s function and general implementation, but the actual execution of it requires managing a lot of special cases - the wrap-around of the underlying storage array, the status of the head and tail when each can be in either numerical relationship to one another depending which has wrapped around recently, overrun/underrun handling, and so forth.
Anonymous
The key to this is establishing conventions, and assumptions that clarify all of the cases before jumping in to try to implement the data accessors. I should have taken a few minutes of design time before writing a line of code. Got bogged down in the weeds while trying to design and code at the same time.
Check out your Company Bowl for anonymous work chats.