-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayADT.c
74 lines (67 loc) · 1.65 KB
/
ArrayADT.c
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
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ArrayADT.h"
const int PRIORITY_COUNT = 16;
// returns the pointer to the list; NULL if list not created
Priority_queue_p createPQ() {
int i = 0;
Priority_queue_p pq_ptr = malloc(sizeof(struct Priority_queue));
for (i = 0; i < PRIORITY_COUNT; i++) {
pq_ptr->Queue_List[i] = createLinkedList();
}
pq_ptr->topPriority = 16;
return pq_ptr;
}
PCB_p search(Priority_queue_p pq) {
if(pq) {
int i = pq->topPriority;
if(i > 15 || i < 0) return NULL;
FIFO_queue_p tempList = pq->Queue_List[i];
//printf("Printf in search(), before dequeue.\n");
struct node* temp = dequeue(tempList);
if(temp == NULL) return NULL;
//printf("Printf in search(), before return.\n");
pq->topPriority = checkTopPriority(pq);
return temp->data;
} else {
printf("!!!pq in search is NULL\n");
return NULL;
}
}
int checkTopPriority(Priority_queue_p pq) {
if(pq) {
int i = 0;
FIFO_queue_p tempList;
for(; i < PRIORITY_COUNT; i++) {
tempList = pq->Queue_List[i];
if(size_is(tempList) > 0) {
return i;
}
}
return -1;
} else {
printf("!!!pq in checkTopPriority is Null");
}
}
void addPCB(Priority_queue_p pq, PCB_p pcb) {
if(pq && pcb) {
int tempPrio = pcb->priority;
if(tempPrio < pq->topPriority) pq->topPriority = tempPrio;
enqueue(pq->Queue_List[tempPrio], pcb);
} else {
printf("Error 358742: Added PCB is NULL.\n");
}
}
int peek(Priority_queue_p pq) {
return pq->topPriority;
}
void freePriority_queue(Priority_queue_p pq) {
int i = 0;
for(i = 0; i < PRIORITY_COUNT; i++) {
freeAll(pq->Queue_List[i]);
}
free(pq);
pq = NULL;
}