-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19_04_simulazione_scadenzaprocura.cpp
More file actions
160 lines (120 loc) · 4.13 KB
/
Copy path19_04_simulazione_scadenzaprocura.cpp
File metadata and controls
160 lines (120 loc) · 4.13 KB
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
#include <iostream>
using namespace std;
template <typename T>
class Node{
private:
T data;
uint clock;
uint expiry;
Node<T>* next;
public:
Node(const T& _data, uint _exp) : data(_data), clock(0), expiry(_exp), next(nullptr) {}
const T& get_data() const {return this->data;}
uint get_clock() const {return this->clock;}
uint get_expiry() const {return this->expiry;}
Node<T>* get_next() const {return this->next;}
void set_data(const T& _data) {this->data = _data;}
uint update_clock() {return ++(this->clock);}
void set_next(Node<T>* _next) {this->next = _next;}
};
template <typename T>
class ExpQueue{
private:
Node<T>* front;
size_t size;
public:
ExpQueue() : front(nullptr), size(0) {}
~ExpQueue() {
Node<T>* curr = this->get_front();
while(curr != nullptr){
Node<T>* next = curr->get_next();
delete curr;
curr = next;
}
}
Node<T>* get_front() const {return this->front;}
size_t get_size() const {return this->size;}
void set_front(Node<T>* _front) {this->front = _front;}
bool is_empty() const {return (this->front == nullptr);}
protected:
Node<T>* delete_node(Node<T>* _prev){
Node<T>* del = nullptr;
Node<T>* new_curr = nullptr;
if(_prev == nullptr){
del = this->get_front();
this->set_front(del->get_next());
new_curr = this->get_front();
}
else{
del = _prev->get_next();
_prev->set_next(del->get_next());
new_curr = del->get_next();
}
this->size--;
delete del;
return new_curr;
}
ExpQueue<T>* enqueue(Node<T>* _node) {
Node<T>* prev = nullptr;
Node<T>* curr = this->get_front();
while(curr != nullptr){
curr->update_clock();
if(curr->get_clock() >= curr->get_expiry() || curr->get_clock() >= _node->get_expiry())
curr = delete_node(prev);
else{
prev = curr;
curr = curr->get_next();
}
}
_node->update_clock();
if(_node->get_clock() < _node->get_expiry()){
if(this->is_empty())
this->set_front(_node);
else
prev->set_next(_node);
this->size++;
}
return this;
}
public:
ExpQueue<T>* enqueue(const T& _data, uint _exp){
return this->enqueue(new Node<T>(_data, _exp));
}
ExpQueue<T>* dequeue() {
if(!this->is_empty()){
Node<T>* tmp = this->get_front();
this->set_front(tmp->get_next());
tmp->set_next(nullptr);
this->enqueue(tmp);
this->size--;
}
return this;
}
void print() const {
Node<T>* curr = this->get_front();
cout << "========================= " << this->get_size() << endl;
while(curr != nullptr){
cout << curr->get_data() << " \t| " << curr->get_clock() << "/" << curr->get_expiry() << endl;
curr = curr->get_next();
}
}
};
template <typename T>
ostream& operator<<(ostream& out, const ExpQueue<T>& obj){
obj.print();
return out;
}
int main() {
ExpQueue<string>* Q = new ExpQueue<string>();
Q->enqueue("Alberto", 5); cout << *Q;
Q->enqueue("Brunetto", 3); cout << *Q;
Q->enqueue("Carlito", 6); cout << *Q;
Q->enqueue("Dorotea", 5); cout << *Q;
Q->enqueue("Esmeralda", 3); cout << *Q;
Q->dequeue(); cout << *Q;
Q->enqueue("Filiberto", 3); cout << *Q;
Q->enqueue("Girolamo", 1); cout << *Q;
delete Q;
return 0;
}
// Immaginando l'uso della coda per dei pacchetti di rete le scadenze dovrebbero essere sempre crescenti (?)