forked from huoyang11/userpro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
executable file
·78 lines (59 loc) · 1.23 KB
/
queue.c
File metadata and controls
executable file
·78 lines (59 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
#include "queue.h"
#include <stdlib.h>
#include <string.h>
int queue_init(struct queue *q) {
if (q == NULL) {
return -1;
}
memset(q, 0, sizeof(struct queue));
return 0;
}
int queue_push(struct queue *q, struct queue_node *n) {
if (q == NULL || n == NULL) {
return -1;
}
memset(n, 0, sizeof(struct queue_node));
if (q->length == 0) {
q->last = n;
q->head = n;
} else {
n->next = q->head;
q->head->prev = n;
q->head = n;
}
q->length++;
return 0;
}
int queue_isempty(struct queue *q) {
if (q == NULL) {
return -1;
}
if (q->length == 0) {
return 1;
}
return 0;
}
int queue_pop(struct queue *q, struct queue_node **n) {
if (q == NULL) {
return -1;
}
if (q->length == 0) {
if (n != NULL) {
*n = NULL;
}
return -2;
}
if (n != NULL) {
*n = q->last;
}
q->last = q->last->prev;
q->length--;
return 0;
}
int queue_uninit(struct queue *q) {
if (q == NULL) {
return -1;
}
memset(q, 0, sizeof(struct queue));
return 0;
}