-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCQueue.c
More file actions
87 lines (77 loc) · 1.53 KB
/
CQueue.c
File metadata and controls
87 lines (77 loc) · 1.53 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
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#define TRUE 0
#define FALSE 1
typedef struct _cqueue_ {
int front, rear;
int maxItems, numElms;
void **elms;
} CQueue;
int incCirc(int i, int maxItems) {
if (i == maxItems - 1) {
return 0;
}
return i + 1;
};
int decCric(int i, int maxItems) {
if (i == 0) {
return maxItems - 1;
}
return i - 1;
};
CQueue *cQueueCreate(int maxItems) {
CQueue *cq;
if (maxItems > 0) {
cq = (CQueue *)malloc(sizeof(CQueue));
if (cq != NULL) {
cq->elms = (void **)malloc(sizeof(void *) * maxItems);
if (cq->elms) {
cq->front = 0;
cq->rear = -1;
cq->maxItems = maxItems;
cq->numElms = 0;
return TRUE;
}
}
}
return NULL;
};
int cEqueue(CQueue *qc, void *elm) {
if (qc != NULL && qc->numElms < qc->maxItems) {
qc->rear = incCirc(qc->rear, qc->maxItems);
qc->elms[qc->rear] = elm;
qc->numElms++;
return TRUE;
}
return FALSE;
};
void *cDequeue(CQueue *qc) {
void *elm;
if (qc != NULL && qc->numElms > 0) {
elm = qc->elms[qc->front];
qc->front = incCirc(qc->front, qc->maxItems);
qc->numElms--;
return elm;
}
return NULL;
};
int cqIsEmpty(CQueue *qc) {
if (qc != NULL && qc->numElms == 0) {
return TRUE;
}
return FALSE;
};
int cqDestroy(CQueue *qc) {
if (qc != NULL && qc->numElms == 0) {
free(qc->elms);
free(qc);
return TRUE;
}
return FALSE;
};
void *cqFirst(CQueue *qc) {
if (qc != NULL && qc->numElms > 0) {
return qc->elms[qc->front];
}
return NULL;
};