blob: b439d4a6010d369a3e3b36faf587e97c3a9eca15 (
plain) (
blame)
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
|
#pragma once
// We represent a singly linked list
// with an pointer to its first element.
template <class T>
struct node
{
T data;
node* next;
node(const T& data_, node* next_ = nullptr) :
data(data_),
next(next_) {}
};
template <class T>
void append_back(node<T>*& head, node<T>*& tail, node<T>* element)
{
if(head == nullptr)
{
head = tail = element;
}
else
{
tail->next = element;
tail = tail->next;
}
}
template <class T>
bool is_sorted(node<T>* list)
{
if(!list || !list->next)
return true;
node<T>* prev = list;
node<T>* next = list->next;
while(next)
{
if(prev->data > next->data)
{
return false;
}
prev = prev->next;
next = next->next;
}
return true;
}
template <class T>
void free_list(node<T>* ll)
{
while(ll)
{
node<T>* to_delete = ll;
ll = ll->next;
delete to_delete;
}
}
template <class T>
unsigned list_size(node<T>* list)
{
unsigned size = 0;
while(list)
{
++size;
list = list->next;
}
return size;
}
template <class T>
node<T>* push_front(node<T>* head, const T& data)
{
return new node<T>(data, head);
}
|