Skip to content

Instantly share code, notes, and snippets.

@iwagaki
Created February 19, 2015 16:54
Show Gist options
  • Save iwagaki/cd2380277200317d7500 to your computer and use it in GitHub Desktop.
Save iwagaki/cd2380277200317d7500 to your computer and use it in GitHub Desktop.
LIFO converts to FIFO using a recursive function
#include <list>
#include <cstdio>
using namespace std;
class LIFO {
public:
void push_back(int a) {
mList.push_back(a);
}
int pop_back() {
int a = mList.back();
mList.pop_back();
return a;
}
int size() {
return mList.size();
}
private:
list<int> mList;
};
class FIFO {
public:
void push_back(int a) {
mLIFO.push_back(a);
}
int pop_first() {
int a = mLIFO.pop_back();
if (mLIFO.size() == 0)
return a;
int b = pop_first();
mLIFO.push_back(a);
return b;
}
private:
LIFO mLIFO;
};
int main() {
FIFO fifo;
for (int i = 0; i < 10; ++i)
fifo.push_back(i);
for (int i = 0; i < 10; ++i)
printf("%d\n", fifo.pop_first());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment