Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. template<class T>
  5. struct List {
  6.     T a;
  7.     List<T>* prev, * next;
  8.  
  9.     List() {
  10.         prev = next = NULL;
  11.     }
  12.  
  13.     List(T value) {
  14.         a = value;
  15.         prev = next = NULL;
  16.     }
  17.  
  18.     void push_front(T value) {
  19.         List<T>* nv = new List<T>(value);
  20.         nv->next = this;
  21.         nv->prev = NULL;
  22.         if (prev == NULL)
  23.             prev = nv;
  24.         else {
  25.             prev->next = nv;
  26.             nv->prev = prev;
  27.             prev = nv;
  28.         }
  29.     }
  30.  
  31.     void push_back(T value) {
  32.         List<T>* nv = new List<T>(value);
  33.         nv->prev = this;
  34.         nv->next = NULL;
  35.         if (next == NULL)
  36.             next = nv;
  37.         else {
  38.             next->prev = nv;
  39.             nv->next = next;
  40.             next = nv;
  41.         }
  42.     }
  43.  
  44.     void pop_back() {
  45.         if (next != NULL) {
  46.             List<T>* temp = next;
  47.             if (next->next != NULL) {
  48.                 next->next->prev = this;
  49.                 next = next->next;
  50.             }
  51.             else
  52.                 next = NULL;
  53.             delete temp;
  54.         }
  55.     }
  56.  
  57.     void pop_front() {
  58.         if (prev != NULL) {
  59.             List<T>* temp = prev;
  60.             if (prev->prev != NULL) {
  61.                 prev->prev->next = this;
  62.                 prev = prev->prev;
  63.             }
  64.             else
  65.                 prev = NULL;
  66.             delete temp;
  67.         }
  68.     }
  69. };
  70.  
  71. template<class T>
  72. struct Stack {
  73. private:
  74.     int _size;
  75.     List<T> *back;
  76. public:
  77.     Stack() {
  78.         _size = 0;
  79.         back = new List<T>();
  80.     }
  81.  
  82.     void push(T value) {
  83.         _size++;
  84.         back->push_back(value);
  85.         back = back->next;
  86.     }
  87.  
  88.     T pop() {
  89.         if (back->prev == NULL)
  90.             throw std::out_of_range();
  91.         T res = back->a;
  92.         back = back->prev;
  93.         back->pop_back();
  94.         _size--;
  95.         return res;
  96.     }
  97.  
  98.     int size() {
  99.         return _size;
  100.     }
  101. };
  102.  
  103. template<class T>
  104. struct Queue {
  105. private:
  106.     int _size;
  107.     List<T>* back, *front;
  108. public:
  109.     Queue() {
  110.         _size = 0;
  111.         front = back = new List<T>();
  112.     }
  113.  
  114.     void push(T value) {
  115.         if (front == back) {
  116.             front = new List<T>(value);
  117.             front->next = back;
  118.             back->prev = front;
  119.         }
  120.         else {
  121.             back->push_front(value);
  122.         }
  123.         _size++;
  124.     }
  125.  
  126.     T pop() {
  127.         if (front->next == NULL) {
  128.             throw std::out_of_range();
  129.         }
  130.         T res = front->a;
  131.         front = front->next;
  132.         front->pop_front();
  133.         _size--;
  134.         return res;
  135.     }
  136.  
  137.     int size() {
  138.         return _size;
  139.     }
  140. };
  141.  
  142.  
  143.  
  144.  
  145.  
  146. void queue_work() {
  147.     Queue<int> sample;
  148.     std::cout << "Queue working: write here how many commands you'll write: ";
  149.     int n;
  150.     std::cin >> n;
  151.     std::cout << "Ok, commands're easy: + <value> to add, - to pop and write in console, s - get size\n";
  152.     for (int i = 0; i < n; ++i) {
  153.         char c;
  154.         int v;
  155.         std::cin >> c;
  156.         if (c == '+') {
  157.             std::cin >> v;
  158.             sample.push(v);
  159.         }
  160.         else if (c == '-') {
  161.             std::cout << sample.pop() << '\n';
  162.         }
  163.         else if (c == 's') {
  164.             std::cout << sample.size() << '\n';
  165.         }
  166.     }
  167. }
  168.  
  169. void stack_work() {
  170.     Stack<int> sample;
  171.     std::cout << "Stack working: write here how many commands you'll write: ";
  172.     int n;
  173.     std::cin >> n;
  174.     std::cout << "Ok, commands're easy: + <value> to add, - to pop and write in console, s - get size \n";
  175.     for (int i = 0; i < n; ++i) {
  176.         char c;
  177.         int v;
  178.         std::cin >> c;
  179.         if (c == '+') {
  180.             std::cin >> v;
  181.             sample.push(v);
  182.         }
  183.         else if (c == '-') {
  184.             std::cout << sample.pop() << '\n';
  185.         }
  186.         else if (c == 's') {
  187.             std::cout << sample.size() << '\n';
  188.         }
  189.     }
  190. }
  191.  
  192. int main() {
  193.     char c;
  194.     std::cout << "Help for you: 1 - Queue, 2 - Stack, 3 - Help, 4 - Clear, 5 - Exit\n";
  195.     while (std::cin >> c) {
  196.         switch (c)
  197.         {
  198.         case '1':
  199.             queue_work();
  200.             break;
  201.         case '2':
  202.             stack_work();
  203.             break;
  204.         case '3':
  205.             std::cout << "Help for you: 1 - Queue, 2 - Stack, 3 - Help, 4 - Clear, 5 - Exit\n";
  206.             break;
  207.         case '4':
  208.             system("cls");
  209.             break;
  210.         case '5':
  211.             exit(0);
  212.             break;
  213.         default:
  214.             break;
  215.         }
  216.     }
  217.     return 0;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement