Home > Back-end >  A value of 3221225477 for help
A value of 3221225477 for help

Time:04-16

Data structure with a queue implementation train carriages rearrangement

# include "LinkQueue. CPP
"# include & lt; Iostream>
using namespace std;

Int main ()
{
Int k, n, leave=1, nowout=1;
cout<& lt;" Please enter the number of buffer rail: ";
Cin> k;
LinkQueue The queue (k + 1), inqueue, outqueue;
cout<& lt;" Please enter the orbit of the carriage number: ";
Cin> n;
cout<& lt;" Please enter a front to the rear of the car number: ";
for(int i=1; i<=n; I++)
{
int num;
Cin> Num.
Inqueue. The EnQueue (num);
}
for(int i=1; i{
int x;
X=inqueue. DeQueue ();
The queue [I] the EnQueue (x);
While (x{
X=inqueue. DeQueue ();
The queue [I] the EnQueue (x);
}
}


while(! Inqueue. Empty ())
{
Int neither=1;
If (inqueue GetQueue ()==nowout)
{
Int x=inqueue. DeQueue ();
Outqueue. The EnQueue (x);
Nowout++;
continue;
}
For (int j=1; j{
If (queue [j] GetQueue ()==nowout)
{
Int x=queue [j] to DeQueue ();
Outqueue. The EnQueue (x);
Nowout++;
Neither=0;
If (queue [j]. Journal of Empty ()) leave++;
}
}
If (neither)
{
Int exist=0, quenum, num;
For (int j=1; j{
If (queue [j]. Journal of Rear () & lt; Inqueue. GetQueue ())
{
Exist=1;
Num=queue [j]. Journal of Rear ();
Quenum=j;
break;
}
}
If (exist==1)
{
For (int j=quenum; jIf (queue [j]. Journal of Rear () & lt; Inqueue. GetQueue () & amp; & The queue [j]. Journal of Rear () & gt; Num)
{
Num=queue [j]. Journal of Rear ();
Quenum=j;
}
Int x=inqueue. DeQueue ();
The queue [quenum] the EnQueue (x);
}
If (exist==0)
{
If (leave> 1)
For (int j=1; j{
If (queue [j]. Journal of Empty ())
{
Int x=inqueue. DeQueue ();
The queue [j]. The EnQueue (x);
Leave -;
}
}
The else {cout<& lt;" Cars can't rearrangement "; break; }
}
}
}

cout<& lt;" Rearrangement after the car number in the order: ";
while (! Outqueue. Empty ())
{
cout}
}


Chain queues:
# include & lt; Iostream>
using namespace std;

The template & lt; The class DT>
Struct Node
{
DT data;
Node
* next;
};

The template & lt; The class DT>
The class LinkQueue
{
Public:
LinkQueue ();
~ LinkQueue () {}
Void the EnQueue (DT) x;
DT to DeQueue ();
DT GetQueue () {return front - & gt; Next - & gt; The data; }
DT Rear () {return Rear - & gt; The data; }
Int the Empty () {if (front==rear) return 1; else return 0; }
Private:
Node
* front, rear;
};

The template & lt; The class DT>
LinkQueue
: : LinkQueue ()
{
Node
* s=new Node
;
S - & gt; Next=NULL;
The front rear of==s;
}

Template
Void LinkQueue
: : the EnQueue (DT) x
{
Node
* s=new Node
;
S - & gt; data=https://bbs.csdn.net/topics/x;
Rear - & gt; Next=s;
Rear=s;
}

The template & lt; The class DT>
DT LinkQueue
: : to DeQueue ()
{
If (rear==front) throw "underflow";
Node
* p=front - & gt; next;
DT x=p - & gt; The data;
The front - & gt; Next=p - & gt; next;
If (p - & gt; Next==NULL) rear=front;
The delete p;
return x;
}



  • Related