-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleForwardList.h
187 lines (161 loc) · 3.92 KB
/
SimpleForwardList.h
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* SimpleForwardList.h
*
* Created on: 19 мая 2019 г.
* Author: sveta
*/
#pragma once
#include <iostream>
#include <iterator>
#include <memory>
template<typename T>
struct Node {
private:
using data_type = T;
public:
//узел для однонаправленного списка.
Node(const Node<T> &node) :
data(node.data), next(node.next) {
}
Node(T &&dt) : data(std::forward<T>(dt)) {
next = nullptr;
}
template<typename ...Args>
Node(Args &&...args) : data(std::forward<Args>(args)...) {
next = nullptr;
}
~Node() = default;
// Значение в узле.
T data;
// Ссылка на следующий элемент.
Node *next;
};
template<typename T>
struct NodeIterator {
// итератор по листу.
private:
using data_type = T;
public:
using iterator_category = std::forward_iterator_tag;
using value_type = Node<T>;
using difference_type = std::ptrdiff_t;
using pointer = value_type * ;
using reference = data_type & ;
NodeIterator(value_type *p) :
p(p) {
}
NodeIterator(const NodeIterator<data_type> & it) :
p(it.p) {
}
bool operator!=(const NodeIterator<data_type> & other) const {
return p != other.p;
}
bool operator==(const NodeIterator<data_type>& other) const {
return p == other.p;
}
reference operator*() {
return (*p).data;
}
NodeIterator<data_type>& operator++() {
p = p->next;
return *this;
}
NodeIterator<data_type> operator++(int) {
NodeIterator tmp = *this;
++(*this);
return tmp;
}
~NodeIterator() = default;
private:
pointer p;
};
// Однонаправленный список.
template<typename T, typename Allocator = std::allocator<Node<T>>>class SimpleForwardList {
public:
using value_type = Node<T>;
using data_type = T;
using allocator_type = Allocator;
using size_type = typename Allocator::size_type;
using const_reference = const data_type&;
using iterator = NodeIterator<data_type>;
using const_iterator = NodeIterator<const data_type>;
SimpleForwardList(const SimpleForwardList& someList)
: head(someList.head),
tail(someList.tail),
_size(someList._size),
_allocator(someList._allocator) {
};
SimpleForwardList(SimpleForwardList&& someList)
: head(std::move(someList.head)),
tail(std::move(someList.tail)),
_size(std::move(someList._size)),
_allocator(std::move(someList._allocator)) {
someList.Init();
}
SimpleForwardList(const allocator_type &alloc = allocator_type()) : _allocator(alloc) {
Init();
}
template<typename ...Args>
void push_back(Args &&...args) {
if (head == nullptr && _size == 0) {
head = _allocator.allocate(1);
_allocator.construct(head, std::forward<Args>(args)...);
tail = head;
}
else {
tail->next = _allocator.allocate(1);
_allocator.construct(tail->next, std::forward<Args>(args)...);
tail = tail->next;
}
_size++;
}
iterator begin() noexcept {
return iterator(this->head);
}
const_iterator cbegin() const noexcept {
return const_iterator(this->head);
}
iterator end()noexcept {
return iterator(this->tail);
}
const_iterator cend() const noexcept {
return const_iterator(this->tail);
}
void pop_head() {
if (head != nullptr) {
auto temp = head;
head = head->next;
_allocator.destroy(temp);
_allocator.deallocate(temp, 1);
_size--;
}
}
void print() {
for (auto i = this->begin(); i != this->end(); i++) {
std::cout << *i << std::endl;
}
if (this->end() != nullptr) {
std::cout << *this->end() << std::endl;
}
}
size_type size() {
return _size;
}
~SimpleForwardList() {
auto current = head;
while (current != nullptr) {
pop_head();
current = head;
}
}
private:
size_type _size;
Node<T> *head;
Node<T> *tail;
allocator_type _allocator;
void Init() {
head = nullptr;
tail = head;
_size = 0;
}
};