blob: 7869103f00e9fa05551fdb84b2328b64d4e734ed (
plain)
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
|
#include <stdlib.h>
#include "queue.h"
void enqueue(queue_t *q, local_id id, timestamp_t time) {
if( q == NULL )
return;
node_t *node = (node_t *) malloc(sizeof(*node));
node->id = id;
node->time = time;
node->next = NULL;
if(q->front == NULL && q->rear == NULL){
q->front = q->rear = node;
return;
}
node_t *current = q->front;
node_t *previous = NULL;
while(current != NULL) {
if( current->time > time ||
( current->time == time && id < current->id ) ) {
node->next = current;
if( previous )
previous->next = node;
if( current == q->front )
q->front = node;
node = NULL;
break;
} else {
previous = current;
current = current->next;
}
}
if( node ) {
q->rear->next = node;
q->rear = node;
node = NULL;
}
}
void dequeue(queue_t *q) {
struct Node* temp = q->front;
if(q->front == NULL)
return;
if(q->front == q->rear)
q->front = q->rear = NULL;
else
q->front = q->front->next;
free(temp);
}
node_t *front(queue_t *q) {
if( q == NULL )
return NULL;
return q->front;
}
queue_t *queue() {
queue_t *q = (queue_t*) malloc(sizeof(*q));
if( q == NULL )
return NULL;
q->front = NULL;
q->rear = NULL;
return q;
}
void free_queue(queue_t *q) {
if( q == NULL )
return;
struct Node* temp = q->front;
while(temp != NULL) {
temp = temp->next;
free(temp);
}
free(q);
}
|