-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.cpp
More file actions
299 lines (249 loc) · 7.87 KB
/
linkedlist.cpp
File metadata and controls
299 lines (249 loc) · 7.87 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "linkedlist.h"
// L1: raw event stream - stores sensor readings exactly as received, no filtering
// Storage rule: Data_stored = Data_sensor
// Time Complexity: O(n)
void List::insertRawEvent(Event val) {
sllNode* nn = new sllNode(val);
if(L1 == NULL) {
L1 = nn;
nn->next = NULL;
}
else {
sllNode* temp = L1;
while(temp->next != NULL) {
temp = temp->next;
}
temp->next = nn;
nn->next = NULL;
}
cout << "L1: Raw Event Inserted at end!" << endl;
}
// time complexity : O(n)
void List::showL1() {
cout << "L1 Raw Stream:" << endl;
sllNode* temp = L1;
while(temp != NULL) {
cout << "Zone " << temp->data.zone << " | Value: " << temp->data.value << endl;
temp = temp->next;
}
}
// L2: verified event stream - noise removed before storing
// Noise rule: |Value_i - Value_i-1| >= delta => noise => reject
// Invalid: value < 0 OR value > 100 => reject
// Time Complexity: O(n)
void List::storeVerifiedEvent(Event val, int delta) {
if(val.value < 0 || val.value > 100) {
cout << "L2:Event Rejected, Value - out of bounds, must be between 1 and 100." << val.value << endl;
return;
}
if(L2 != NULL) {
sllNode* temp = L2;
while(temp->next != NULL) {
temp = temp->next;
}
int prev = temp->data.value;
int diff = val.value - prev;
if(diff < 0)
diff = -diff; // absolute value
if(diff >= delta) {
cout << "L2: Noise Detected -> |" << val.value << " - " << prev << "| = " << diff << " >= delta(" << delta << ") -> Rejected" << endl;
return;
}
}
sllNode* nn = new sllNode(val);
if(L2 == NULL) {
L2 = nn;
nn->next = NULL;
}
else {
sllNode* temp = L2;
while(temp->next != NULL) {
temp = temp->next;
}
temp->next = nn; // append at end
nn->next = NULL;
}
cout << "L2: Verified Event Stored -> Zone " << val.zone << " | Value: " << val.value << endl;
}
// O(n)
void List::showL2() {
cout << "L2 Verified Stream:" << endl;
sllNode* temp = L2;
while(temp != NULL) {
cout << "Zone " << temp->data.zone << " | Value: " << temp->data.value << endl;
temp = temp->next;
}
}
// L3: anomaly stream - stores readings that deviate too far from normal
// Anomaly condition: |Value_current - Value_normal| > theta
// Normal = 25 (normal forest temperature)
// Time Complexity: O(n)
void List::storeAnomalyEvent(Event val, int theta) {
float normal = 25;
float diff = abs(val.value - normal);
if(diff <= theta) {
cout << val.value << " is not anomalous. Not stored." << endl;
return;
}
sllNode* nn = new sllNode(val);
if(L3 == NULL) {
L3 = nn;
cout << "L3: Anomaly Stored -> Zone " << val.zone << " | Value: " << val.value << endl;
return;
}
sllNode* cur = L3;
while(cur->next != NULL)
cur = cur->next;
cur->next = nn;
cout << "L3: Anomaly Stored -> Zone " << val.zone << " | Value: " << val.value << endl;
}
// O(n)
void List::showL3() {
cout << "L3 Anomaly Stream:" << endl;
sllNode* temp = L3;
while(temp != NULL) {
cout << "Zone " << temp->data.zone << " | Value: " << temp->data.value << endl;
temp = temp->next;
}
}
// L4: forward correction chain , when past event corrected -> propagate change forward
// Event(i+1) = f(Event(i)) -> alpha increases +0.5 each step
// Time Complexity: O(n)
void List::forwardCorrection(int zone, int newValue) {
dllNode* temp = dllHead;
if(!temp)
return;
float alpha = newValue;
while(temp != NULL) {
if(temp->data.zone == zone) {
temp->data.value = newValue;
dllNode* forward = temp->next;
while(forward != NULL) {
alpha += 0.5;
forward->data.value = alpha;
forward = forward->next;
}
cout << "L4: Forward Correction Applied! " << endl;
return;
}
temp = temp->next;
}
cout << "L4: Zone not found for correction!" << endl;
}
// L5: backward correction chain , when new correct value found, fix previous events
// Event(i-1) = f(Event(i)) -> beta decreases -0.5 each step back
// Time Complexity: O(n)
void List::backwardCorrection(int zone, int newValue) {
dllNode* temp = dllHead;
while(temp != NULL) {
if(temp->data.zone == zone) {
temp->data.value = newValue;
dllNode* back = temp->prev;
float beta = newValue;
while(back != NULL) {
beta -= 0.5;
back->data.value = beta;
back = back->prev;
}
cout << "L5: Backward Correction Applied" << endl;
return;
}
temp = temp->next;
}
cout << "L5: Zone not found for correction!" << endl;
}
// L6: state synchronization - all nodes = global base value
// Consistency rule: Event_all = Event_base
// Time Complexity: O(n)
void List::syncSystem() {
if(L1 == NULL) return;
int baseValue = L1->data.value;
sllNode* temp = L1;
while(temp != NULL) {
temp->data.value = baseValue;
temp = temp->next;
}
cout << "L6: System Synchronized" << endl;
}
// circular list insert - tail->next = head (loop)
// Time Complexity: O(1)
void List::insertCircular(Event val) {
sllNode* nn = new sllNode(val);
if(tail == NULL) {
tail = nn;
tail->next = tail;
}
else {
nn->next = tail->next;
tail->next = nn;
tail = nn;
}
}
// L7: local monitoring - watch one zone in repeating cycle
// Cycle rule: Monitor(zone) -> repeat
// Time Complexity: O(n)
void List::localMonitor(int zone) {
if(tail == NULL)
return;
sllNode* temp = tail->next; // head
cout << "L7: Local Monitoring Zone " << zone << endl;
do {
if(temp->data.zone == zone) {
cout << "Value: " << temp->data.value << endl;
}
temp = temp->next;
} while(temp != tail->next);
}
// L8: system monitoring - scan all zones one after another
// Scan rule: sum of all zones
// Time Complexity: O(n)
void List::systemMonitor() {
if(tail == NULL)
return;
sllNode* temp = tail->next; // head
cout << "L8: System Monitoring All Zones" << endl;
do {
cout << "Zone " << temp->data.zone << " | Value: " << temp->data.value << endl;
temp = temp->next;
}
while(temp != tail->next);
}
// L9: emergency monitoring - activated when value > 50
// Trigger: Value > Threshold => Emergency
// Time Complexity: O(n)
void List::emergencyMonitor() {
if(tail == NULL)
return;
sllNode* temp = tail->next; // tail->next = head
cout << "L9: EMERGENCY MODE ACTIVATED!!!" << endl;
do {
if(temp->data.value > 50) {
cout << "ALERT -> Zone " << temp->data.zone << endl;
}
temp = temp->next;
}
while(temp != tail->next);
}
// L10: stability monitoring - check if values changing too much
// Stability condition: |Value_t - Value_t-1| < epsilon => stable
// Time Complexity: O(n)
void List::stabilityMonitor(int eps) {
if(tail == NULL)
return;
sllNode* temp = tail->next; // head
sllNode* prev = NULL;
cout << "L10: Stability Monitor (eps=" << eps << ")" << endl;
do {
if(prev != NULL) {
int diff = temp->data.value - prev->data.value;
if(diff < 0) diff = -diff;
if(diff < eps)
cout << "Zone " << temp->data.zone << " -> STABLE (change=" << diff << ")" << endl;
else
cout << "Zone " << temp->data.zone << " -> UNSTABLE (change=" << diff << ")" << endl;
}
prev = temp;
temp = temp->next;
}
while(temp != tail->next);
}