-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSAQueue.c
66 lines (53 loc) · 1.42 KB
/
SAQueue.c
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
/* Copyright (C) 2011 by Chenguang Wang(wecing)
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "SimpleAV.h"
#include <stdlib.h>
SAQContext *SAQ_init(void)
{
SAQContext *p = (SAQContext *)malloc(sizeof(SAQContext));
if(p == NULL)
return p;
p->head = p->tail = NULL;
p->nb = 0;
return p;
}
int SAQ_push(SAQContext *saq_ptr, void *data)
{
_SAQNode *node_ptr = (_SAQNode *)malloc(sizeof(_SAQNode));
if(node_ptr == NULL)
return -1;
node_ptr->next = NULL;
node_ptr->data = data;
if(saq_ptr->tail != NULL)
saq_ptr->tail->next = node_ptr;
saq_ptr->tail = node_ptr;
if(saq_ptr->head == NULL)
saq_ptr->head = node_ptr;
saq_ptr->nb++;
return 0;
}
void *SAQ_pop(SAQContext *saq_ptr)
{
if(saq_ptr->nb == 0)
return NULL;
_SAQNode *head_ptr = saq_ptr->head;
void *data = head_ptr->data;
if(saq_ptr->nb == 1)
saq_ptr->tail = NULL;
saq_ptr->head = head_ptr->next;
saq_ptr->nb--;
free(head_ptr);
return data;
}
#ifdef __cplusplus
}
#endif