-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_queue.cpp
82 lines (61 loc) · 1.63 KB
/
linked_queue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "assignment/linked_queue.hpp"
namespace assignment {
LinkedQueue::~LinkedQueue() {
// эквивалентно очистке очереди
LinkedQueue::Clear();
}
void LinkedQueue::Enqueue(int value) {
// Write your code here ...
}
bool LinkedQueue::Dequeue() {
// Write your code here ...
return false;
}
void LinkedQueue::Clear() {
// Write your code here ...
}
std::optional<int> LinkedQueue::front() const {
// Write your code here ...
return std::nullopt;
}
std::optional<int> LinkedQueue::back() const {
// Write your code here ...
return std::nullopt;
}
bool LinkedQueue::IsEmpty() const {
return false;
}
int LinkedQueue::size() const {
return 0;
}
// ДЛЯ ТЕСТИРОВАНИЯ
LinkedQueue::LinkedQueue(const std::vector<int>& values) {
if (values.empty()) {
return;
}
auto* curr_node = new Node(values.front());
front_ = curr_node;
for (int index = 1; index < values.size() - 1; ++index) {
curr_node->next = new Node(values[index]);
curr_node = curr_node->next;
}
if (values.size() == 1) {
back_ = front_;
} else {
curr_node->next = new Node(values.back());
back_ = curr_node->next;
}
size_ = static_cast<int>(values.size());
}
std::vector<int> LinkedQueue::toVector() const {
if (front_ == nullptr || size_ == 0) {
return {};
}
std::vector<int> array;
array.reserve(size_);
for (auto* node = front_; node != nullptr; node = node->next) {
array.push_back(node->value);
}
return array;
}
} // namespace assignment