-
Notifications
You must be signed in to change notification settings - Fork 15
/
cqueue.c
78 lines (65 loc) · 1.16 KB
/
cqueue.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
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
#include "cqueue.h"
void init_queue(cqueue_t * q, int size)
{
q->count = q->front = 0;
q->rear = -1;
q->max = size;
q->ele = (void **)calloc(size, sizeof(void*));
}
static int is_full(cqueue_t * q)
{
return (q->count == q->max);
}
static int is_empty(cqueue_t * q)
{
return (q->count == 0);
}
void *queue_front(cqueue_t * q)
{
if(is_empty(q)) {
return NULL;
}
return q->ele[q->front];
}
void queue_push(cqueue_t * q, void *item)
{
if( is_full(q) ) {
/*printf("Queue Overflow\n");*/
q->count--;
}
q->rear = (q->rear+1) % q->max;
q->ele[q->rear] = item;
q->count++;
}
void * queue_pop(cqueue_t * q)
{
void *item;
if(is_empty(q)){
/*printf("Queue empty\n");*/
return NULL;
}
item = q->ele[q->front];
q->front = (q->front+1) % q->max;
q->count--;
return item;
}
/*
#define MAX 3
int main()
{
int i,item=0;
cqueue_t q;
int val[6];
init_queue(&q, MAX);
for(i=0; i < 6; i++){
val[i] = i * 10;
queue_push(&q, (void*)&val[i]);
}
for(i=0; i < MAX; i++){
printf("%d\n", *(int*)q.ele[i]);
}
return 0;
}
*/