Created
November 3, 2015 03:06
-
-
Save jen6/0f0102afc74be6bdf415 to your computer and use it in GitHub Desktop.
두개 큐로 스택 구현하기
This file contains hidden or 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 <iostream> | |
#include <queue> | |
#include <string> | |
#include <exception> | |
using namespace std; | |
queue<int> qu1, qu2; | |
enum : int { QUEUE_MIN = 0 ,QUEUE_MAX = 10}; | |
void push(int val) | |
{ | |
if(qu1.size() == QUEUE_MAX) | |
{ | |
throw "stack overflow"; | |
} | |
qu1.push(val); | |
std::cout << val << " pushed" << std::endl; | |
} | |
int pop() | |
{ | |
int ret; | |
if(qu1.size() == QUEUE_MIN) | |
{ | |
throw "stack underflow"; | |
} | |
for(size_t i = qu1.size(); i > 1; --i) | |
{ | |
qu2.push( qu1.front() ); | |
qu1.pop(); | |
} | |
ret = qu1.front(); | |
qu1.pop(); | |
for(size_t i = qu2.size(); i > 0; --i) | |
{ | |
qu1.push( qu2.front() ); | |
qu2.pop(); | |
} | |
return ret; | |
} | |
int main() | |
{ | |
int val, option; | |
try{ | |
while(true) | |
{ | |
std::cout << "1.push 2.pop 3.end" << std::endl; | |
std::cin >> option; | |
switch(option) | |
{ | |
case 1: | |
std::cout << "push : "; | |
std::cin >> val; | |
push(val); | |
break; | |
case 2: | |
std::cout << "pop : " << pop() << std::endl; | |
break; | |
case 3: | |
return 0; | |
break; | |
default: | |
std::cout << "no options" << std::endl; | |
} | |
} | |
}catch(const char * e) { | |
std::cerr << e << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ㄷ