Your basic problem seems to be the first condition:
if (index>=elements|| index<=0)
In the first run index will always be <= 0 (you set it to 0 yourself)
Change the condition to:
if (index>=elements|| index<0)
EDIT: But Richard's point is also valid and to whoever downvoted him I say: :thumbsdown: and also: X|