-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
83 lines (77 loc) · 1.72 KB
/
queue.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
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <stdlib.h>
#include "headers.h"
node *newnode(int processID, int burstTime, int priority)
{
node *n = (node *)malloc(sizeof(node));
n->processID = processID;
n->burstTime = burstTime;
n->currentBurstTime = burstTime;
n->priority = priority;
n->waitTime = 0;
n->turnAroundTime = 0;
n->next = NULL;
return n;
}
Queue *createQueue()
{
Queue *newqueue = (Queue *)malloc(sizeof(Queue));
newqueue->front = newqueue->rear = NULL;
return newqueue;
}
// void enqueue(Queue *q, int processID, int burstTime, int priority)
// {
// node *temp = newnode(processID, burstTime, priority);
// if (q->rear == NULL)
// {
// q->rear = q->front = temp;
// }
// else
// {
// q->rear->next = temp;
// q->rear = temp;
// }
// }
node *dequeue(Queue *q)
{
if (q->front == NULL)
{
return NULL;
}
else
{
node *temp = q->front;
q->front = q->front->next;
if (q->front == NULL)
{
q->rear = NULL;
}
return temp;
}
}
/* Prints the queue in the following format:
* Process ID, Burst Time, Wait Time, Turnaround Time, Priority
*/
void showQueue(Queue *q)
{
node *ptr = q->front;
if (ptr == NULL)
{
printf("EMPTY\n");
return;
}
printf("Process ID\tBurst Time\tWait Time\tTurnaround Time\tPriority\n");
while (ptr != NULL)
{
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", ptr->processID, ptr->burstTime, ptr->waitTime, ptr->turnAroundTime, ptr->priority);
ptr = ptr->next;
}
printf("\n");
}
void deleteQueue(Queue *q)
{
node *temp;
while((temp = dequeue(q)) != NULL)
free(temp);
free(q);
}