Before in article C ++ Stack Example Rearranging RailRoad Cars train compartment rescheduling problems we use three stacks to solve the "Rearranging RailRoad Cars" problem. in this article, we will use two queues to solve the problem.
The Whole Code
[Cpp]
// RailRoadQueue. cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <iostream>
# Include <queue>
Using namespace std;
Template <class T>
Void PrintfNum (T a [], const int & n );
Bool Hold (queue <int> q [], int n, int totalQueue ){
For (int I = 0; I <totalQueue; ++ I ){
If (q [I]. empty () | (! Q [I]. empty () & q [I]. back () <n )){
Cout <"holding track" <I <"hold car" <n <endl;
Q [I]. push (n );
Return true; // we already find a holding track, so break the loop.
}
}
Return false;
}
Void OutPut (queue <int> q [], int n, int totalQueue, int & min ){
For (int I = 0; I <totalQueue; ++ I ){
If (! Q [I]. empty () & q [I]. front () = min ){
Cout <"Move car" <q [I]. front () <"from holding track" <I <"to output" <endl;
Q [I]. pop ();
++ Min;
I =-1; // find next car from the first holding track 0
}
}
}
Int main (int argc, char * argv [])
{
Const int NUM = 9;
Const int QUEUENUM = 2;
Queue <int> q [QUEUENUM];
Int min = 1;
Int a [NUM] = {5, 8, 6, 3 };
PrintfNum (a, NUM );
For (int I = NUM-1; I> = 0; -- I ){
If (a [I] = min ){
Cout <"Move car" <a [I] <"from input to output" <endl;
++ Min;
// Move cars from holding tracks
OutPut (q, a [I], QUEUENUM, min );
} Else {// move cars to holding tracks
If (! Hold (q, a [I], QUEUENUM )){
Cout <"Not enough holding track" <endl;
Break;
}
}
}
Return 0;
}
Template <class T>
Void PrintfNum (T a [], const int & n ){
For (int I = 0; I <n; ++ I ){
Cout <a [I] <",";
}
Cout <endl;
}
// RailRoadQueue. cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <iostream>
# Include <queue>
Using namespace std;
Template <class T>
Void PrintfNum (T a [], const int & n );
Bool Hold (queue <int> q [], int n, int totalQueue ){
For (int I = 0; I <totalQueue; ++ I ){
If (q [I]. empty () | (! Q [I]. empty () & q [I]. back () <n )){
Cout <"holding track" <I <"hold car" <n <endl;
Q [I]. push (n );
Return true; // we already find a holding track, so break the loop.
}
}
Return false;
}
Void OutPut (queue <int> q [], int n, int totalQueue, int & min ){
For (int I = 0; I <totalQueue; ++ I ){
If (! Q [I]. empty () & q [I]. front () = min ){
Cout <"Move car" <q [I]. front () <"from holding track" <I <"to output" <endl;
Q [I]. pop ();
++ Min;
I =-1; // find next car from the first holding track 0
}
}
}
Int main (int argc, char * argv [])
{
Const int NUM = 9;
Const int QUEUENUM = 2;
Queue <int> q [QUEUENUM];
Int min = 1;
Int a [NUM] = {5, 8, 6, 3 };
PrintfNum (a, NUM );
For (int I = NUM-1; I> = 0; -- I ){
If (a [I] = min ){
Cout <"Move car" <a [I] <"from input to output" <endl;
++ Min;
// Move cars from holding tracks
OutPut (q, a [I], QUEUENUM, min );
} Else {// move cars to holding tracks
If (! Hold (q, a [I], QUEUENUM )){
Cout <"Not enough holding track" <endl;
Break;
}
}
}
Return 0;
}
Template <class T>
Void PrintfNum (T a [], const int & n ){
For (int I = 0; I <n; ++ I ){
Cout <a [I] <",";
}
Cout <endl;
}
While two holding track are sufficient to rearrange the cars from the initial ordering of gif picture. however, other initial arrangements may need more tracks. for example, the in‑arrangement 1, 2, 3, 4, 5, 6, 7, 8, 9 requires 8 holding tracks.