-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.hpp
54 lines (46 loc) · 848 Bytes
/
list.hpp
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
// This file is covered by the LICENSE file in the root of this project.
#pragma once
#include "io.hpp"
#include <cstddef>
#include <utility>
template<typename T>
struct Node
{
const T data;
Node* next = nullptr;
explicit Node(T data) : data(std::move(data))
{}
};
// Reads the lists, returns the pointer to the last element in the list
template<typename T>
Node<T>* read_list(std::size_t size, Node<T>*& head)
{
head = nullptr;
auto last = head;
while (size-- > 0)
{
T data;
read(data);
const auto n = new Node(std::move(data));
if (head != nullptr)
{
last->next = n;
last = n;
}
else
head = last = n;
}
return last;
}
template<typename T>
void write(const Node<T>* head)
{
if (head == nullptr)
return;
write(head->data);
while (head != nullptr)
{
write(' ', head->data);
head = head->next;
}
}