-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
97 lines (86 loc) · 1.77 KB
/
Copy pathqueue.c
File metadata and controls
97 lines (86 loc) · 1.77 KB
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
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include <unistd.h>
#include <getopt.h>
#include "schedutils.h"
void push(Queue* queue, Instruction instr){
Node* n = (Node*) malloc(sizeof(Node));
n->pointer = instr;
n->next = NULL;
if(queue->head == NULL || n->pointer->priority > queue->head->pointer->priority){
n->next = queue->head;
queue->head = n;
queue->size++;
}
else{
Node* temp = queue->head;
Node* p = temp->next;
while(p != NULL){
if(p->pointer->priority < n->pointer->priority){
n->next = p;
temp->next = n;
queue->size++;
return;
}
p = p->next;
temp = temp->next;
}
n->next = NULL;
temp->next = n;
queue->size++;
}
}
Instruction pop (Queue* queue){
Node* head = queue->head;
Instruction node = head->pointer;
queue->head = head->next;
queue->size--;
free(head);
return node;
}
Instruction peek(Queue* queue){
Node* head = queue->head;
if(head== NULL)
return NULL;
return head->pointer;
}
Instruction removeNode(Queue* queue, Instruction instr){
Node* head = queue->head;
if(head == NULL){
return NULL;
}
else if(head->next == NULL && head->pointer!= instr)
return NULL;
else if(head->next == NULL && head->pointer == instr){
queue->head = head->next;
queue->size--;
free(head);
puts("remove success");
return instr;
}
while(head->next != NULL){
Instruction node = head->next->pointer;
if(node == instr){
Node* temp = head->next;
head->next = temp->next;
queue->size--;
free(temp);
puts("remove success");
return node;
}
head = head->next;
}
return NULL;
}
Queue createQueue() {
Queue queue;
queue.size = 0;
queue.head = NULL;
queue.tail = NULL;
queue.pop = &pop;
queue.push = &push;
queue.peek = &peek;
queue.removeNode = &removeNode;
return queue;
}