So based on your assignment .A garage is actually a better example of a "stack" rather then a "queue". A stack operates as first in last out, where a queue is first in first out. now if we are certin that your garage has only one entry/exit point then the idea of pulling the cars 1 & 2 out, and then removing car 3 is correct. As what you discribed, it would be better if instead of using a random temp queue you used your street as a temp queue (if possible) if not, then you could use only one temp queue you dont need 3 different temp queues.
Here is an example:
lets say you have 4 cars that arrive in this order :
(0056)(0048)(0087)(0032)
your Scratch-em-up queue look like this:
1 2 3
|
[(0056)][(0048)][(0087)][(0032)][(empty)] // Scratch-em-up queue
0 1 2 3 4
// you might also want to defind empty it would make it easier.
|
At this point your front should be pointing at
position[0]
& your rear should be at
position[3]
.
Then lets say
car (0087)
would like to depart. So use your street to move out (copy)
cars(0056) & (0048)
in it. Maybe have a push & pop functions to help copy/move cars to empty the queue so that the
car(0087)
can depart .What pop function could do is remove cars from the queue and return the car being removed and replace that position with empty. Also a push function which will insert car into queue. So your main/depart function maybe would say something like:
1 2
|
street_queue.push(Scratch-em-up.pop());// street_queue could also be the temp queue
count_move++;// keeps count of the moves
|
Now, all you have to do have a function called depart which will search for the car that needs to depart, then move the cars that are blocking em and removed the car from the garage.
you also might want to have a repark function which reparks the cars.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
[(0056)][(0048)][(0087)][(0032)][(empty)] // Scratch-em-up queue (arrive)
0 1 2 3 4
[(0056)][(0048)][(empty)][(empty)][(empty)] // temp queue (temp park)
0 1 2 3 4
[(empty)][(empty)][(0087)][(0032)][(empty)] // Scratch-em-up queue(remove the car
0 1 2 3 4 //that needs to move)
[(empty)][(empty)][(empty)][(0032)][(empty)] // Scratch-em-up queue(car removed)
0 1 2 3 4
[(empty)][(empty)][(empty)][(empty)][(empty)] // temp queue(re park cars back
0 1 2 3 4 //to Scratch-em-up queue
[(0056)][(0048)][(empty)][(0032)][(empty)] // Scratch-em-up queue
0 1 2 3 4
[(0056)][(0048)][(0032)][(empty)][(empty)] // Scratch-em-up queue (repark by
0 1 2 3 4 //moving (0032) to position[2]
//- insert(4 cars)
//- pop & push from garage to temp
//- pop from garage the car that needs to depart
//- pop & push from temp to garage
//- repark (resort) garage.
|
Note:that you front and rear will never rap around the queue, meaning your Front or rear will never pass maxqueue which is 5 cars.
Hope this gives you some kind of idea?