-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcess Manager.cpp
120 lines (99 loc) · 1.82 KB
/
Process Manager.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
/**
* author : coderstack
**/
#include "bits/stdc++.h"
using namespace std;
vector<int> order;
struct node
{
int i, data;
node *next;
node(int i, int data)
{
this->i = i;
this->data = data;
next = NULL;
}
};
node *insertProcess(node *head, int i)
{
int d;
cin >> d;
node *p = new node(i, d);
if (!head)
{
head = p;
head->next = head;
}
else
{
node *t = head;
while (t->next != head)
t = t->next;
t->next = p;
p->next = head;
}
return head;
}
node *deleteProcess(node *n)
{
if (n->next != n && n->data == 0)
{
node *t = n->next;
n->data = t->data;
n->i = t->i;
n->next = t->next;
delete t;
}
return n;
}
node *computeTask(node *n)
{
if (n->data > 10)
{
n->data -= 10;
}
else
{
n->data = 0;
order.emplace_back(n->i);
n = deleteProcess(n);
}
return n;
}
void solve(node *head)
{
int time = 0;
node *t = head;
while (t != NULL)
{
if (t->data == 0)
break;
time += 10;
int id = t->i;
t = computeTask(t);
if(id == t->i)
t = t->next;
}
cout << "Time taken to finish all the above processes : " << time << '\n';
cout << "Order in which tasks were executed :\n";
vector<int> :: iterator it;
for(it = order.begin(); it != order.end(); it++)
cout << *it << " ";
}
signed main()
{
cin.tie(NULL)->sync_with_stdio(false);
cout << "Enter number of processes\n";
int n, i = 1;
cin >> n;
cout << "Enter time of each process\n";
node *head = NULL;
while (n--)
{
head = insertProcess(head, i);
i += 1;
}
solve(head);
return 0;
}