Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. using namespace std;
  5.  
  6. class FIFO {
  7. public:
  8. void push(int value) {
  9. main_stack.push(value);
  10. }
  11.  
  12. int pop() {
  13. int result = 0;
  14. while (!main_stack.empty() && main_stack.size() > 1) {
  15. help_stack.push((int &&) main_stack.top());
  16. main_stack.pop();
  17. }
  18. result = main_stack.top();
  19. main_stack.pop();
  20. while (!help_stack.empty()) {
  21. main_stack.push((int &&) help_stack.top());
  22. help_stack.pop();
  23. }
  24. return result;
  25. }
  26.  
  27. unsigned long size() {
  28. return this->main_stack.size();
  29. }
  30.  
  31. private:
  32. stack<int> main_stack;
  33. stack<int> help_stack;
  34. };
  35.  
  36. int main() {
  37. FIFO fifo;
  38. fifo.push(4);
  39. fifo.push(3);
  40. fifo.push(2);
  41. fifo.push(1);
  42. cout << fifo.pop(); // Powinno wypisać 4
  43. fifo.push(4);
  44. fifo.push(3);
  45. fifo.push(2);
  46. fifo.push(1);
  47. cout << fifo.pop(); // Powinno wypisać 3 jeszcze z poprzedniego dodania
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement