I developed a method called "rotate" for my class of class objects. I did what if there are elements in the stack: {0,2,3,4,5,6,7}, I would have to rotate the elements back and forth.
Where, if I need to turn forward 2 elements, then we get {3,4,5,6,7,0,2} in the array. And if I need to turn back or -3 elements, then, looking at the original array, it will be {5,6,7,0,2,3,4}
So the method that I developed works fine. Its just terribly ineffective IMO. I was wondering if I can combine an array using a mod statement? Or, if their useless code hanged around something that I did not understand yet, and so on.
I think my question is: how can I simplify this method? for example using less code .:-)
void stack::rotate(int r)
{
int i = 0;
while ( r > 0 )
{
front.n = items[top+1].n;
for ( int j = 0; j < bottom; j++ )
{
items[j] = items[j+1];
}
items[count-1].n = front.n;
r--;
}
while ( r < 0 )
{
if ( i == top+1 )
{
front.n = items[top+1].n;
items[top+1].n = items[count-1].n;
}
back.n = items[++i].n;
items[i].n = front.n;
if ( i == bottom )
{
items[count-1].n = front.n;
i = 0;
r++;
continue;
}
else
{
front.n = items[++i].n;
items[i].n = back.n;
if ( i == bottom )
{
i = 0;
r++;
continue;
}
}
}
}