-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularDoublyLinkedListAssignment.cpp
209 lines (167 loc) · 6.13 KB
/
circularDoublyLinkedListAssignment.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
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
#include <iostream>
using namespace std;
// Data Class : Holds all the data that goes inside the Node
class Process {
public:
string processName;
int totalTime;
Process(string processName, int totalTime) {
this->totalTime = totalTime;
this->processName = processName;
}
void updateRunTime() {
totalTime--;
}
void print() {
cout << "Process " << processName << " " ;
cout << totalTime << "seconds" << endl;
}
};
// Node Class : Node for the LinkedList
template <typename T> class Node {
public:
T *data;
Node<T> *next;
Node<T> *prev;
Node(T *data) {
this->data = data;
next = nullptr;
prev = nullptr;
}
void print() { data->print(); }
};
template <typename T> class CircularDLL {
private:
Node<T> *curr;
int length;
public:
// Constructor
CircularDLL(T *value) {
Node<T> *newNode = new Node<T>(value);
curr = newNode;
length = 1;
}
//Destructor
~CircularDLL() {
Node<T> *temp = curr;
while (curr) {
curr = curr->next;
delete temp;
temp = curr;
}
}
void printList() {
if (length == 0) return;
if (length == 1) {
curr->print();
return;
}
Node<T> *temp = curr;
do { // Iterate through circular list, stopping at curr (head)
temp->print();
temp = temp->next;
} while (temp != curr);
}
bool isEmpty() {
if (length <= 0) return true;
else return false;
}
Node<T> *getCurr(){
return curr;
}
int getLength(){
return length;
}
void insertProcess(T *value) {
Node<T> *newNode = new Node<T>(value);
if (length == 0) { // No items, make newNode the new curr
curr = newNode;
} else if (length == 1){ // Only curr and newNode, assign pointers to match each other
curr->prev = newNode;
curr->next = newNode;
newNode->prev = curr;
newNode->next = curr;
} else { // Using curr->prev to access last element in list,
newNode->next = curr; // ...attach newNode to first(curr) and last(curr->prev) node.
curr->prev->next = newNode;
newNode->prev = curr->prev;
curr->prev = newNode;
}
length++; // Increment list length
}
void deleteProcess(Node<T> *temp) {
if (length == 0) return; // If all processes have finished, cannot delete, stop
if (temp == curr) curr = curr->next; // Special case if deleting the head node, ensuring no loss of curr reference
if (length == 2) {
temp->prev->next = nullptr; // length = 2, make pointers of other node null before deletion
temp->next->prev = nullptr; // temp->next and temp->prev point to the same node in a length 2 list
} else if (length > 2) {
temp->prev->next = temp->next; // Assign previous's *next to point to the next node
temp->next->prev = temp->prev; // Assign next's *prev to point to the previous node
}
delete temp; // Free up memory space and decrement length
length--;
}
};
//Main Program
int main() {
Process *d1 = new Process("A", 3);
Process *d2 = new Process("B", 4);
Process *d3 = new Process("C", 4);
Process *d4 = new Process("D", 15);
Process *d5 = new Process("E", 15);
int quantumTime;
int timeElapsed = 0;
int cycleNum = 1;
int length;
int newTime;
string newName;
char choice;
Node<Process> *temp;
Node<Process> *toDelete;
CircularDLL<Process> *cdll = new CircularDLL<Process>(d1);
cout << "Enter Quantum Time: ";
cin >> quantumTime;
cout << endl << "Prepopulating with processes" << endl << endl;
// Prepopulated list inserted
cdll->insertProcess(d2);
cdll->insertProcess(d3);
cdll->insertProcess(d4);
cdll->insertProcess(d5);
// Main system loop
while (!cdll->isEmpty()){ // Program finished when list is empty
cdll->printList();
// Insert node
cout << endl << "Add new process? (Enter Y/N) ";
cin >> choice;
if (choice == 'y' || choice == 'Y'){ // User inserted node by typing "Y"
cout << endl << "Enter New Process Name: ";
cin >> newName;
cout << endl << "Enter Total Process Time: ";
cin >> newTime;
Process *newProcess = new Process(newName, newTime); // Create a new Process object with user parameters
cdll->insertProcess(newProcess); // Call insert method to join into list
cout << endl << "Process Added." << endl << endl;
continue;
}
// Run cycles and Delete nodes
cout << endl << "Running Cycle " << cycleNum << endl << endl;
for (int i = 1; i <= quantumTime; i++) { // Each cycle, 1 second
temp = cdll->getCurr(); // Use temp pointer to iterate through list
length = cdll->getLength(); // Use temp length to avoid problems with list shortening
for (int j = 0; j < length; j++) { // For each node in the current list,...
temp->data->updateRunTime(); // Decrement node's runtime by 1 second
if (temp->data->totalTime <= 0) { // If process has completed(t <= 0),
toDelete = temp; // Create delete proxy to avoid losing list reference
temp = temp->next;
cdll->deleteProcess(toDelete); // Delete node
} else temp = temp->next; // Still time remaining, advance to next node
}
}
timeElapsed += quantumTime;
cout << "After cycle " << cycleNum << " - " << timeElapsed << " second elapses - state of processes is as follows" << endl << endl;
cycleNum++;
}
cout << "All processes are completed." << endl;
return 0;
}