質問

I am writing my own deque/double-ended queue program. The thing I can't seem to figure out where to start in the array. Lets say I have an array of size 6, written a push method to insert left and one to insert right. But pushing items left at index 0 doesn't make much sense as I would have to deal with the wrap around right away.

After some research I found out that if I have an empty array then the first item gets pushed in the middle of the array, and depending on where I want to push elements on the left or the right side of the array, best is to work from the middle? I am just wondering if this is the way to go.

役に立ちましたか?

解決

As I understand, you can use that array circularly. Store two integers storing a start index and an end index; and move them instead of moving the elements in the array. When you reach to either end, just wrap to to other end an continue(Use % length in calculations). With this way, you can start from anywhere you want and never need to move an element. Kinda like Circular Buffers.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top