Pagini recente » Cod sursa (job #498066) | Cod sursa (job #2218263) | Cod sursa (job #1458482) | Cod sursa (job #157146) | Cod sursa (job #1785987)
//Queue
#include <fstream>
using namespace std;
ifstream fin("queue.in");
ofstream fout("queue.out");
#define NMAX 30000
unsigned N, WR, i, X;
unsigned top1, S1[NMAX+5];
unsigned top2, S2[NMAX+5];
char operatie[18];
void getNumber();
void push1();
void pop1();
void push2();
void pop2();
int main()
{
fin>>N;
top1 = top2 = 0;
for(i = 1 ; i <= N ; i++) {
fin>>operatie; //citim operatia
fout<<i<<":"; //afisam indicele operatiei din input
if(operatie[1] == 'u') { //push_back(X)
getNumber(); //functia stocheaza valoarea de introdus in variabila globala X
push1();
fout<<" read("<<X<<") push(1,"<<X<<")";
fout<<'\n';
}
else { //operatie[1] == 'o', adica pop_front()
if(top1 > 0) {
if(top2 == 0) {
while(top1 > 1) {
pop1(); fout<<" pop(1)";
X = WR;
push2(); fout<<" push(2,"<<WR<<")";
}
pop1(); fout<<" pop(1) write("<<WR<<")";
}
else {
pop2(); fout<<" pop(2) write("<<WR<<")";
}
}
else {
pop2(); fout<<" pop(2) write("<<WR<<")";
}
fout<<'\n';
}
}
fin.close();
fout.close();
return 0;
}
void getNumber() {
unsigned i;
X = 0;
for(i = 10 ; operatie[i] != ')' ; i++)
X = X*10 + (unsigned)(operatie[i]-'0');
}
void push1() {
if(top1 < NMAX)
S1[++top1] = X;
}
void pop1() {
if(top1 > 0)
WR = S1[top1--];
}
void push2() {
if(top2 < NMAX)
S2[++top2] = X;
}
void pop2() {
if(top2 > 0)
WR = S2[top2--];
}