blob: 381f628eac5314da7b799e6c82ca22ea2d35f010 (
plain)
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
|
#pragma once
/**
* Representation of a task in the system
*/
struct Task {
const char *name;
int id;
int priority;
int burst;
};
#ifdef __cplusplus
template <typename T> struct Queue {
struct Item {
Item(T *node) : node(node) {}
T *node;
Item *next = nullptr;
[[nodiscard]] bool
operator==(const T *other) const
{
return node == other;
}
};
~Queue() noexcept
{
for (auto *it = head; it != nullptr;) {
auto *current = it;
it = it->next;
delete current->node;
delete current;
}
}
/// Insert item at the end of the queue
void
insert(T *item)
{
if (head == nullptr) {
head = new Item(item);
tail = head;
}
else {
tail->next = new Item(item);
tail = tail->next;
}
}
void
remove(T *item)
{
if (head == nullptr) return;
if (item == head->node) {
auto *it = head;
head = head->next;
if (*tail == item) tail = nullptr;
delete it;
return;
}
Item *prev = nullptr;
for (auto *it = head; it != nullptr; it = it->next) {
if (it->node == item) {
if (prev) { prev->next = it->next; }
if (tail == it) { tail = prev; }
delete it;
return;
}
prev = it;
}
}
Item *head = nullptr;
Item *tail = nullptr;
};
#endif
|