-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ3_Lab10_using_linklist.cpp
More file actions
70 lines (57 loc) · 1.82 KB
/
Q3_Lab10_using_linklist.cpp
File metadata and controls
70 lines (57 loc) · 1.82 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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class ComputerTask {
public:
int id;
int priority;
ComputerTask* next;
ComputerTask(int taskId, int taskPriority) {
id = taskId;
priority = taskPriority;
next = NULL;
}
};
void insertTask(ComputerTask*& head, int taskId, int taskPriority) {
ComputerTask* newTask = new ComputerTask(taskId, taskPriority);
if (head == NULL || head->priority < taskPriority ||
(head->priority == taskPriority && head->id > taskId)) {
newTask->next = head;
head = newTask;
return;
}
ComputerTask* current = head;
while (current->next != NULL &&
(current->next->priority > taskPriority ||
(current->next->priority == taskPriority && current->next->id < taskId))) {
current = current->next;
}
newTask->next = current->next;
current->next = newTask;
}
void executeTask(ComputerTask*& head) {
if (head == NULL) return;
ComputerTask* taskToExecute = head;
head = head->next;
cout << "Executing Task " << taskToExecute->id << " with Priority " << taskToExecute->priority << endl;
delete taskToExecute;
}
int main() {
srand(time(0));
int taskCount;
cout << "Enter the number of tasks to schedule: ";
cin >> taskCount;
ComputerTask* taskList = NULL;
cout << "\nGenerated tasks with their priorities:\n";
for (int i = 0; i < taskCount; i++) {
int taskPriority = rand() % 10 + 1;
insertTask(taskList, i + 1, taskPriority);
cout << "Task " << i + 1 << " has Priority: " << taskPriority << endl;
}
cout << "\nTask Execution Order (Highest Priority First):\n";
while (taskList != NULL) {
executeTask(taskList);
}
return 0;
}