0

Im trying to shift my array left as efficiently as possible. Im using pointers now, and im having trouble assigning the values back into my array:

void stack::rotate(int nRotations)
{
   if ( count <= 1 ) return;

   int *intFrontPtr = &items[top+1].n;
   int *intBackPtr  = &items[count-1].n;
   int temp = 0;

   for (int shift = 0; nRotations != 0 ;)
   {
        if ( nRotations > 0 ) // we rotate left
        {  
             temp = *++intFrontPtr; // give temp the value
             items[++shift].n = temp; // debug shows success
             if ( shift == count ) // dont overrun array
             {
                 temp = *intBackPtr;
                 items[count-1].n = temp;
                 shift = 0; // reset for another rotation
                 nRotations--; // decrement we have reached the end
             }
        }

    } 
}
3
  • 1
    You'd probably get more help if you accepted answers more than just 21% of the time. Commented Nov 1, 2009 at 4:23
  • And also if he did not just repeated the same question over and over... Commented Nov 1, 2009 at 14:03
  • Too much is not given to answer this... For example, where is "top" defined... What is "n"? Commented Nov 1, 2009 at 16:49

1 Answer 1

4

c++ has a function built in in <algorithm>. Just call std::rotate(front_ptr, front_ptr + N, back_ptr);

Sign up to request clarification or add additional context in comments.

1 Comment

referring to the items address might be the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.