-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.c
More file actions
78 lines (69 loc) · 1.32 KB
/
Queue.c
File metadata and controls
78 lines (69 loc) · 1.32 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 <stdio.h>
#include <stdlib.h>
#define TRUE 0
#define FALSE 1
typedef struct _queue_ {
int front, rear;
int maxItems;
void **items;
} Queue;
Queue *qCreate(int maxItems) {
Queue *q;
if (maxItems > 0) {
q = (Queue *)malloc(sizeof(Queue));
if (q != NULL) {
q->items = (void **)malloc(sizeof(void *) * maxItems);
if (q->items != NULL) {
q->maxItems = maxItems;
q->front = 0;
q->rear = -1;
return q;
}
}
free(q);
}
return NULL;
};
int qDestroy(Queue *q) {
if (q != NULL && q->rear < 0) {
free(q->items);
free(q);
return TRUE;
}
return FALSE;
};
Queue *qFirst(Queue *q) {
if (q != NULL && q->rear >= 0) {
return q->items[q->front];
}
return NULL;
};
int qIsEmpty(Queue *q) {
if (q != NULL && q->rear < 0) {
return TRUE;
}
return FALSE;
};
int qEnqueue(Queue *q, void *data) {
if (q != NULL && q->rear < q->maxItems - 1) {
q->rear++;
q->items[q->rear] = data;
return TRUE;
}
return FALSE;
};
void *qDequeue(Queue *q) {
void *excItem;
int cur, next;
if (q != NULL && q->rear >= 0) {
excItem = q->items[q->front];
for (int i = 0; i < q->rear - 1; i++) {
cur = i;
next = i++;
q->items[cur] = q->items[next];
}
q->rear--;
return excItem;
}
return NULL;
};