Posted by hussein on August 06, 2000 at 00:57:37:
hi
heres another piece of code to look at (this one i know works because i tried it)
Queue Q, T; // data and temp queue
int count=0;
while (!Q.Empty ()) // count elements
T.EnQue (Q.DeQue ());
while (!T.Empty ())
{
Q.EnQue (T.DeQue ());
count++;
}
// store front element, cycle rest and insert front into correct position try it out and see if it works ok ... whats interesting about this algorithm is that all you need the second queue for is to count the elements - if you could get a count some other way (((rear-front) % MAXQUE)?), you dont even need the second queue... and for both solutions i posted there is a tradeoff: speed for memory - they use fewer data structures but take longer to run - which is a counter-example to illustrate the whole reason we structure data: a little more structure goes a long way in making our programs faster and easier to write/understand ... and theres even one more moral to this story ... many problems that initially seem impossible can actually be solved with proper inspiration (or enough perspiration :))
for ( int i=count-1; i>0; i-- )
{
ItemType item = Q.DeQue ();
for ( int j=0; j Q.EnQue (Q.DeQue ());
Q.EnQue (item);
for ( int k=0; k
}