Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication8.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include "iostream"
- #include "string"
- #include "sstream"
- #include "Windows.h"
- #include "iomanip"
- template <typename T>
- class queue
- {
- struct Node
- {
- T value;
- size_t priority;
- Node *next, *prev;
- };
- Node *head, *tail;
- public:
- queue()
- {
- head = tail = nullptr;
- }
- queue(const queue& q)
- {
- Node* helpPtr(q.tail);
- while (helpPtr) {
- push(helpPtr->value, helpPtr->priority);
- helpPtr = helpPtr->prev;
- }
- }
- ~queue()
- {
- Node* helpPtr(head);
- while (helpPtr) {
- Node* buf(helpPtr);
- delete helpPtr; helpPtr = buf;
- }
- }
- void push(T val, size_t prt)
- {
- Node* newNode(new Node);
- newNode->value = val;
- newNode->priority = prt;
- if (head) {
- Node* helpPtr(head);
- while (helpPtr && helpPtr->priority < prt)
- helpPtr = helpPtr->next;
- if (!helpPtr) {
- newNode->next = nullptr;
- tail->next = newNode;
- newNode->prev = tail;
- tail = newNode;
- return;
- }
- if (helpPtr == head) {
- newNode->prev = nullptr;
- head->prev = newNode;
- newNode->next = head;
- head = newNode;
- return;
- }
- newNode->prev = helpPtr->prev;
- newNode->next = helpPtr->next;
- helpPtr->prev->next = newNode;
- helpPtr->prev = newNode;
- newNode->next = helpPtr;
- }
- else {
- newNode->prev = newNode->next = nullptr;
- head = tail = newNode;
- }
- }
- T pop()
- {
- T val(tail->value);
- delete tail;
- return val;
- }
- void print()
- {
- Node* helpPtr(head);
- std::cout << "{ ";
- while (helpPtr) {
- std::cout << helpPtr->value << " ";
- helpPtr = helpPtr->next;
- }
- std::cout << "}" << std::endl;
- }
- void print()
- const {
- Node* helpPtr(head);
- std::cout << "{ ";
- while (helpPtr) {
- std::cout << helpPtr->value << " ";
- helpPtr = helpPtr->next;
- }
- std::cout << "}" << std::endl;
- }
- void clear()
- {
- Node* helpPtr(head);
- while (helpPtr) {
- Node* buf(helpPtr->next);
- delete helpPtr; helpPtr = buf;
- }
- head = tail = nullptr;
- }
- queue& operator= (const queue& q)
- {
- clear();
- Node* helpPtr(q.tail);
- while (helpPtr) {
- push(helpPtr->value, helpPtr->priority);
- helpPtr = helpPtr->prev;
- }
- return *this;
- }
- friend const queue operator + (const queue& q1, const queue& q2){
- queue newQ(q1);
- Node* helpPtr(q2.tail);
- while (helpPtr) {
- newQ.push(helpPtr->value, helpPtr->priority);
- helpPtr = helpPtr->prev;
- }
- return queue(newQ);
- }
- };
- /*template <typename T>
- class printer
- {
- queue<T> userName, text;
- size_t count;
- public:
- printer() { count = 0; }
- void send(std::string u, std::string s, size_t p)
- {
- userName.push(u, p);
- text.push(s, p);
- count++;
- }
- void print()
- {
- std::cout << "Printing:" << std::endl;
- for (size_t i = 0; i < count; i++) {
- std::cout << "Username: " << userName.pop() << std::endl;
- std::cout << "Text: " << text.pop() << std::endl;
- }
- count = 0;
- }
- };*/
- int main()
- {
- system("color F0");
- queue<int> a, b, d, z;
- a.push(2, 0);
- a.push(4, 3);
- a.push(1, 0);
- a.push(3, 2);
- std::cout << "a: "; a.print();
- b.push(1, 4);
- const queue< int> c(a);
- c.print();
- d = a;
- std::cout << "b: "; b.print();
- z = a + b;
- std::cout << "c = "; c.print();
- std::cout << "a: "; a.print();
- std::cout << "b: "; b.print();
- std::cout << "d: "; d.print();
- /*c = a * b;
- std::cout << "a * b = "; c.print();
- printer<std::string> q;
- q.send("User 3", "You", 1);
- q.send("User 1", "Are", 2);
- q.send("User 2", "How", 3);
- q.send("User 3", "?", 1);
- q.print();*/
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement