-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.cpp
170 lines (152 loc) · 3.75 KB
/
queue.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
#include "queue.h"
#include "process.h"
#include "stack.h"
#include <cstdio>
Queue::Queue() : size(0) {
head = NULL;
tail = NULL;
}
// enqueues one process to tail
void Queue::Enqueue(Process *p) {
Node *newNode = new Node;
newNode->next = NULL;
newNode->p = p;
size++;
if(head != NULL) {
tail->next = newNode;
tail = tail->next;
} else {
head = newNode;
tail = newNode;
}
}
// dequeues process with a specific pid
Process* Queue::Dequeue(int pid) {
Node *parent;
Node *current = head;
bool pExists = false;
while(current != NULL) {
if(current->p->GetPid() == pid) {
pExists = true;
break;
}
parent = current;
current = current->next;
}
if(pExists) {
Process *p = current->p;
if(current == head) {
Node *t = head;
head = head->next;
delete t;
} else if(current == tail) {
tail = parent;
tail->next = NULL;
delete current;
} else {
parent->next = current->next;
delete current;
}
size--;
return p;
} else {
printf("Error while dequeing process\n");
return NULL;
}
}
// dequeues head process
Process* Queue::Dequeue() {
if(size == 1) {
Process *p = head->p;
delete head;
head = NULL;
tail = NULL;
size--;
return p;
} else if(size > 1) {
Process *p = head->p;
Node *current = head;
head = head->next;
delete current;
size--;
return p;
}
return NULL;
}
Node* Queue::GetHead() { return head; }
int Queue::GetSize() { return size; }
bool Queue::IsEmpty() { return size == 0; }
// LRUStack data structure for LRU
LRUStack::LRUStack() : size(0) {
head = NULL;
tail = NULL;
}
// pushes on top of the stack
void LRUStack::Push(int p, int a) {
LRU *n = new LRU();
n->pid = p;
n->allocId = a;
n->prev = NULL;
if(size == 0) {
n->next = NULL;
n->prev = NULL;
head = n;
tail = n;
} else {
n->next = head;
head->prev = n;
head = n;
}
size++;
}
// pops specific memory pid & allocid
LRU* LRUStack::Pop(int pid, int alloc) {
if(size > 0) {
LRU *parent;
LRU *current = head;
while(current != NULL) {
if((current->pid == pid) && (current->allocId == alloc)) {
break;
}
parent = current;
current = current->next;
}
size--;
if(size == 0) {
head = NULL;
tail = NULL;
current->next = NULL;
current->prev = NULL;
return current;
} else {
if(current == head) {
LRU *temp = current;
current = current->next;
current->prev = NULL;
temp->next = NULL;
head = current;
return temp;
} else if(current == tail) {
parent->next = NULL;
current->prev = NULL;
tail = parent;
return current;
} else {
current->next->prev = parent;
parent->next = current->next;
current->next = NULL;
current->prev = NULL;
return current;
}
}
}
return NULL;
}
// pops tail and returns tail pid and allocid
LRU* LRUStack::GetLeastUsed() {
return Pop(tail->pid, tail->allocId);
}
LRU* LRUStack::GetTail() { return tail; }
LRU* LRUStack::GetHead() { return head; }
int LRUStack::GetSize() { return size; }
bool LRUStack::IsEmpty() { return size == 0; }