I had a previous thread about pushing data in a circular queue.
I didnt see a need to keep that thread going.
After all the items are pushed into the circular queue
i need to rotate them forward and reverse.
Problem is i cant use a temporary array to do it.
I need to do it inside the circular que.
could i use a bitwise to do it?
This is some of my code.
Code:nt main (void) { quack q(QUACK_SIZE); #ifdef _WIN32 // request memory leak report in Output Window after main returns _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); #endif cout << q; push(q, true, 1); push(q, true, 2); push(q, true, 3); push(q, true, 4); push(q, false, 0); push(q, true, 9); cout << q; cout << "--- # of items: " << q.itemCount() << endl << endl; pop(q, true); cout << q; pop(q, true); cout << q; push(q, false, 7); cout << q; push(q, false, 8); cout << q; cout << ">>> rotate(2)\n"; q.rotate(2); cout << q; cout << ">>> rotate(-3)\n"; q.rotate(-3); cout << q; cout << ">>> reverse\n"; q.reverse();
Code:class quack { public: quack(int capacity); ~quack(void); bool pushFront(const int n); // push an item onto the front bool pushBack(const int n); // push an item onto the back bool popFront(int& n); // pop an item off the front bool popBack(int& n); // pop an item off the back void rotate(int r); // "rotate" the stored items (see note below) void reverse(void); // reverse the order of the stored items int itemCount(void); // return the current number of stored items private: int front; int back; int maxsize; int count; struct item // definition of each item stored by the quack { int n; }; item *items; // pointer to storage for the circular arrayAnd i need to rotate them by the number being passed in.Code:bool quack::pushFront(const int n) { if ( count == maxsize ) { return false; } front = (front - 1 ) % maxsize; items[front].n = n; count++; return true; } bool quack::pushBack(const int n) { back = (back = front + count ) % maxsize; items[back].n = n; count++; return true; } bool quack::popFront(int& n) { front = (front + 1 ) % maxsize; --count; return true; } bool quack::popBack(int& n) { back = (back -1) % maxsize; --count; return true; }
Right now the circular array looks like:Code:void quack::rotate(int r) { } void quack::reverse(void) { }
3,2,1,0,7,8 before the rotate.
Rotate(2)
1,0,7,8,3,2
Any hints on how to accomplish this?



LinkBack URL
About LinkBacks


