-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshotQueue.c
More file actions
68 lines (51 loc) · 1.71 KB
/
shotQueue.c
File metadata and controls
68 lines (51 loc) · 1.71 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
#include "shotQueue.h"
ShotQueue* createShotQueue() {
ShotQueue *shotQueue = malloc(sizeof(ShotQueue));
shotQueue->head = malloc(sizeof(ShotNode));
shotQueue->head->next = shotQueue->head;
shotQueue->head->shot = NULL;
shotQueue->lastNode = shotQueue->head;
shotQueue->first = NULL;
shotQueue->last = NULL;
return shotQueue;
}
void removeShotNode(ShotNode *node, ShotQueue *st) {
ShotNode *actualNode = st->head;
while (actualNode->next != st->head) {
if (actualNode->next == node) {
actualNode->next = node->next;
if (actualNode->next == st->head) {
st->lastNode = actualNode;
st->last = actualNode->shot;
}
st->first = st->head->next->shot;
freeShot(node->shot);
free(node);
return;
}
actualNode = actualNode->next;
}
}
void enqueueShot(Shot *st, ShotQueue *shotQueue) {
ShotNode *newNode = malloc(sizeof(ShotNode));
newNode->shot = st;
newNode->next = shotQueue->head;
shotQueue->lastNode->next = newNode;
shotQueue->lastNode = newNode;
shotQueue->last = shotQueue->lastNode->shot;
shotQueue->first = shotQueue->head->next->shot;
}
Shot* dequeueShot(ShotQueue *shotQueue) {
ShotNode *firstNode = shotQueue->head->next;
Shot *st = firstNode->shot;
shotQueue->head->next = firstNode->next;
shotQueue->first = shotQueue->head->next->shot;
if (firstNode == shotQueue->lastNode) {
shotQueue->lastNode = shotQueue->head;
shotQueue->last = NULL;
}
return st;
}
BOOL isShotQueueEmpty(ShotQueue *shotQueue) {
return shotQueue->first == NULL;
}