Created
February 19, 2015 16:54
-
-
Save iwagaki/cd2380277200317d7500 to your computer and use it in GitHub Desktop.
LIFO converts to FIFO using a recursive function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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