Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cpp
- #include <iostream>
- #include <string>
- #include "queue.cpp"
- using namespace std;
- void reverse (queue <string> &q)
- {
- if (q.empty()) return;
- else
- {
- string x = q.front();
- q.pop();
- reverse(q);
- q.push(x);
- }
- }
- void print (queue <string> q)
- {
- while (!q.empty())
- {
- cout << q.front() << " ";
- q.pop();
- }
- cout << '\n';
- }
- int main()
- {
- queue <string> q;
- cout << "Введите ваши слова: ";
- string word;
- while (cin >> word) q.push(word);
- //останавливаем цикл зажав Ctrl + Z или Ctrl + D
- reverse(q);
- cout << "Измененная очередь: ";
- print(q);
- return 0;
- }
- //queue.cpp
- #include <cassert>
- template <class Type>
- class queue
- {
- struct element
- {
- Type inf;
- element *next;
- element (Type i) : inf(i), next(nullptr) {}
- };
- element *head, *tail;
- public:
- queue() : head(nullptr), tail(nullptr) {}
- bool empty() { return head == nullptr; }
- void push (Type x)
- {
- element *t = tail;
- tail = new element(x);
- if (head == nullptr) head = tail;
- else t -> next = tail;
- }
- Type front() { assert(!empty()); return head -> inf; }
- void pop()
- {
- if (empty()) return;
- else
- {
- element *t = head;
- head = t -> next;
- if (head == nullptr) tail = nullptr;
- delete t;
- }
- }
- };
Add Comment
Please, Sign In to add comment