-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPriorityQueue2.h
111 lines (97 loc) · 1.68 KB
/
PriorityQueue2.h
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
#pragma once
#include "headers.h"
#define MAX_SIZE 10
struct Node2
{
int priority;
struct Process data;
struct Node2 *next;
};
struct PriorityQueue2
{
struct Node2 *front;
int count;
};
typedef struct PriorityQueue2 PQ2;
PQ2 create()
{
PQ2 *Q = (PQ2 *)malloc(sizeof(PQ2));
Q->front = NULL;
Q->count = 0;
return *Q;
}
bool isEmpty2(PQ2 *Q)
{
if (Q->count == 0)
{
return true;
}
else
{
return false;
}
}
int size(PQ2 *Q)
{
return Q->count;
}
void insert(PQ2 *Q, int priority, struct Process data)
{
if (Q->count >= MAX_SIZE)
{
printf("Queue Overflow\n");
return;
}
struct Node2 *temp = (struct Node2 *)malloc(sizeof(struct Node2));
temp->priority = priority;
temp->data = data;
temp->next = NULL;
if (Q->count == 0 || Q->front->priority > temp->priority)
{
temp->next = Q->front;
Q->front = temp;
}
else
{
struct Node2 *p, *q;
q = NULL;
p = Q->front;
while (p && p->priority <= temp->priority)
{
q = p;
p = p->next;
}
temp->next = p;
q->next = temp;
}
Q->count++;
}
struct Process dequeue2(PQ2 *Q)
{
struct Process x;
x.id = -1;
x.arrivalTime = -1;
x.Priority = -1;
x.runTime = -1;
if (isEmpty2(Q))
{
return x;
}
x = Q->front->data;
Q->front = Q->front->next;
Q->count--;
return x;
}
struct Process front(PQ2 *Q)
{
struct Process x;
x.id = -1;
x.arrivalTime = -1;
x.Priority = -1;
x.runTime = -1;
if (isEmpty2(Q))
{
return x;
}
return Q->front->data;
}