C ++ Queue Example Rearranging RailRoad Cars

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.