Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <iostream>
- struct Node {
- int data;
- Node* next;
- Node(int value) : data(value), next(nullptr) {}
- };
- // Очередь целых чисел.
- class Queue {
- public:
- Queue(int size) : head(nullptr), tail(nullptr) {}
- ~Queue() {
- for (Node* c = tail; c != nullptr;) {
- Node* next = c->next;
- delete c;
- c = next;
- }
- }
- // Добавление и извлечение элемента из очереди.
- void enqueue(int a) {
- if (tail == nullptr) {
- tail = head = new Node(a);
- } else {
- head->next = new Node(a);
- head = head->next;
- }
- }
- int dequeue() {
- assert(tail != 0);
- Node* next = tail->next;
- delete tail;
- tail = next;
- if (tail == nullptr) head = nullptr;
- }
- // Проверка на пустоту.
- bool empty() const { return tail == nullptr; }
- private:
- Node* head;
- Node* tail;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement