-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqueue.c
More file actions
80 lines (63 loc) · 1.23 KB
/
queue.c
File metadata and controls
80 lines (63 loc) · 1.23 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
/**********************************
* @author Diego Sosa
* description
**********************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "queue.h"
queue queue_init()
{
queue* q = malloc(sizeof(struct queue));
q->front = NULL;
q->back = NULL;
q->size = 0;
return *q;
}
void queue_destroy(queue q)
{
while(queue_num_size(&q) > 0)
{
dequeue(&q);
}
}
qnode* peek(queue* q)
{
qnode* n = malloc(sizeof(struct qnode));
memcpy(n, q->front, sizeof(struct qnode));
return n;
}
int dequeue(queue* q)
{
if(q->front == NULL && q->back == NULL)
{
return 0;
}
if(queue_num_size(q) == 0){
return 0;
}
// free memory !!!!
q->front = q->front->prev;
free(q->front->next);
q->size = q->size - 1;
return 0;
}
int enqueue(queue* q, void* data)
{
qnode* n = malloc(sizeof(struct qnode));
n->data = data;
if(q->front == NULL && q->back == NULL) // Empty queue
{
q->front = n;
q->back = n;
return 1;
}
q->back->prev = n;
n->next = q->back;
q->back = n;
q->size = q->size + 1;
return 0;
}
int queue_num_size(queue* q){
return q->size;
}