-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.cpp
142 lines (110 loc) · 2.8 KB
/
events.cpp
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
/*
Classes for linked list of events - LLNODE, LIST, and EVENT.
Author: Larry Henschen
Last Modified: Nov 26, 2016 (Sara Sood)
*/
#include "definitions.h"
#include "events.h"
#include <iostream>
using namespace std;
// Functions for class LLNODE. LLNODE is the list node for each element in the list.
// It contains a pointer to the actual data and a pointer to the next node in the list.
LLNODE::LLNODE(EVENT* e) {
ev = e;
next = NULL;
}
LLNODE::~LLNODE() {
delete ev;
}
// Functions for class LIST. LIST contains the pointer to the front of the list.
LIST::LIST() {
front = NULL;
}
LIST::~LIST() {
LLNODE* tmp;
LLNODE* tmpNext;
tmp = front;
while(tmp!=NULL) {
tmpNext = tmp->next;
delete tmp;
tmp = tmpNext;
}
}
void LIST::display() {
LLNODE* tmp; int k;
tmp = front; k = 1;
cout << "Displaying Event list." << endl;
while(tmp!=NULL) {
cout << "Node " << k << ":" << endl;
(tmp->ev)->display();
tmp = tmp->next;
k += 1;
}
}
EVENT* LIST::getFirstEvent() {
if(front==NULL) return NULL;
else return front->ev;
}
void LIST::removeFirstEvent() {
LLNODE* tmp;
if(front==NULL) return; // If empty, do nothing.
tmp = front; // Else remember thee front node,
front = front->next; // move ffront to the second node,
delete tmp; // and then delete the old front node.
}
void LIST::insertEvent(EVENT* e) {
LLNODE* left;
LLNODE* right;
LLNODE* newnode;
int etime, tmptime;
newnode = new LLNODE(e); // Create the new node.
etime = e->getProcessTime();
// If the list is empty, then the new node just becomes the front
// of the list.
if(front==NULL) {
front = newnode;
return;
}
// Otherwise, first find where the new node goes.
left = NULL;
right = front;
while(right!=NULL) {
tmptime = (right->ev)->getProcessTime();
if(etime<tmptime) break;
left = right;
right = right->next;
}
// There are two cases. If left is still NULL, the new node goes at the front
// of the list. Otherwise, the new node goes between left and right. The latter
// case includes the case when the new node goes at the end (right==NULL).
if(left!=NULL) {
// The new node goes between left and right, including goes at the
// end (right==NULL).
left->next = newnode;
newnode->next = right;
}
else {
// The new node goes at the front.
newnode->next = front;
front = newnode;
}
}
// Functions for class EVENT.
EVENT::EVENT(string n, int v, int pt) {
device = n;
value = v;
processTime = pt;
}
void EVENT::display() {
cout << "EVENT: Device = " << device <<
"\nDevice code: " << value << ". Scheduled to be process at : " << processTime << endl;
}
int EVENT::getProcessTime() {
return processTime;
}
string EVENT::getDeviceName() {
return device;
}
int EVENT::getValue() {
return value;
}