Advertisement
Guest User

queue

a guest
Dec 16th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. // ConsoleApplication8.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4.  
  5. #include "stdafx.h"
  6. #include "iostream"
  7. #include "string"
  8. #include "sstream"
  9. #include "Windows.h"
  10. #include "iomanip"
  11.  
  12. template <typename T>
  13. class queue
  14. {
  15.     struct Node
  16.     {
  17.         T value;
  18.         size_t priority;
  19.         Node *next, *prev;
  20.     };
  21.     Node *head, *tail;
  22. public:
  23.     queue()
  24.     {
  25.         head = tail = nullptr;
  26.     }
  27.     queue(const queue& q)
  28.     {
  29.         Node* helpPtr(q.tail);
  30.         while (helpPtr) {
  31.             push(helpPtr->value, helpPtr->priority);
  32.             helpPtr = helpPtr->prev;
  33.         }
  34.     }
  35.     ~queue()
  36.     {
  37.         Node* helpPtr(head);
  38.         while (helpPtr) {
  39.             Node* buf(helpPtr);
  40.             delete helpPtr; helpPtr = buf;
  41.         }
  42.     }
  43.     void push(T val, size_t prt)
  44.     {
  45.         Node* newNode(new Node);
  46.         newNode->value = val;
  47.         newNode->priority = prt;
  48.         if (head) {
  49.             Node* helpPtr(head);
  50.             while (helpPtr && helpPtr->priority < prt)
  51.                 helpPtr = helpPtr->next;
  52.             if (!helpPtr) {
  53.                 newNode->next = nullptr;
  54.                 tail->next = newNode;
  55.                 newNode->prev = tail;
  56.                 tail = newNode;
  57.                 return;
  58.             }
  59.             if (helpPtr == head) {
  60.                 newNode->prev = nullptr;
  61.                 head->prev = newNode;
  62.                 newNode->next = head;
  63.                 head = newNode;
  64.                 return;
  65.             }
  66.             newNode->prev = helpPtr->prev;
  67.             newNode->next = helpPtr->next;
  68.             helpPtr->prev->next = newNode;
  69.             helpPtr->prev = newNode;
  70.             newNode->next = helpPtr;
  71.         }
  72.         else {
  73.             newNode->prev = newNode->next = nullptr;
  74.             head = tail = newNode;
  75.         }
  76.     }
  77.     T pop()
  78.     {
  79.         T val(tail->value);
  80.         delete tail;
  81.         return val;
  82.     }
  83.     void print()
  84.     {
  85.         Node* helpPtr(head);
  86.         std::cout << "{ ";
  87.         while (helpPtr) {
  88.             std::cout << helpPtr->value << " ";
  89.             helpPtr = helpPtr->next;
  90.         }
  91.         std::cout << "}" << std::endl;
  92.     }
  93.     void print()
  94.     const {
  95.         Node* helpPtr(head);
  96.         std::cout << "{ ";
  97.         while (helpPtr) {
  98.             std::cout << helpPtr->value << " ";
  99.             helpPtr = helpPtr->next;
  100.         }
  101.         std::cout << "}" << std::endl;
  102.     }
  103.     void clear()
  104.     {
  105.         Node* helpPtr(head);
  106.         while (helpPtr) {
  107.             Node* buf(helpPtr->next);
  108.             delete helpPtr; helpPtr = buf;
  109.         }
  110.         head = tail = nullptr;
  111.     }
  112.     queue& operator= (const queue& q)
  113.     {
  114.         clear();
  115.         Node* helpPtr(q.tail);
  116.         while (helpPtr) {
  117.             push(helpPtr->value, helpPtr->priority);
  118.             helpPtr = helpPtr->prev;
  119.         }
  120.         return *this;
  121.     }
  122.     friend const queue  operator + (const queue& q1, const queue& q2){
  123.         queue newQ(q1);
  124.         Node* helpPtr(q2.tail);
  125.         while (helpPtr) {
  126.             newQ.push(helpPtr->value, helpPtr->priority);
  127.             helpPtr = helpPtr->prev;
  128.         }
  129.         return queue(newQ);
  130.     }
  131. };
  132. /*template <typename T>
  133. class printer
  134. {
  135.     queue<T> userName, text;
  136.     size_t count;
  137. public:
  138.     printer() { count = 0; }
  139.     void send(std::string u, std::string s, size_t p)
  140.     {
  141.         userName.push(u, p);
  142.         text.push(s, p);
  143.         count++;
  144.     }
  145.     void print()
  146.     {
  147.         std::cout << "Printing:" << std::endl;
  148.         for (size_t i = 0; i < count; i++) {
  149.             std::cout << "Username: " << userName.pop() << std::endl;
  150.             std::cout << "Text: " << text.pop() << std::endl;
  151.         }
  152.         count = 0;
  153.     }
  154. };*/
  155. int main()
  156. {
  157.     system("color F0");
  158.     queue<int> a, b, d, z;
  159.     a.push(2, 0);
  160.     a.push(4, 3);
  161.     a.push(1, 0);
  162.     a.push(3, 2);
  163.     std::cout << "a: "; a.print();
  164.     b.push(1, 4);
  165.     const queue< int> c(a);
  166.     c.print();
  167.     d = a;
  168.     std::cout << "b: "; b.print();
  169.     z = a + b;
  170.     std::cout << "c = "; c.print();
  171.     std::cout << "a: "; a.print();
  172.     std::cout << "b: "; b.print();
  173.     std::cout << "d: "; d.print();
  174.     /*c = a * b;
  175.     std::cout << "a * b = "; c.print();
  176.     printer<std::string> q;
  177.     q.send("User 3", "You", 1);
  178.     q.send("User 1", "Are", 2);
  179.     q.send("User 2", "How", 3);
  180.     q.send("User 3", "?", 1);
  181.     q.print();*/
  182.     system("pause");
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement