cannot find the correct logic for the loop
What loop is that? I don't see where there is a loop for the problem of the index going to -1

Think how the index must be changed to go to the previous item from the current:
If the current index is 2 then the index of the previous item is 1.
If the current index is 0 then the index must wrap to the end of the array.
(I'm assuming that you want next and previous to wrap around when the end of the array is reached.)