-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
58 lines (49 loc) · 909 Bytes
/
queue.c
File metadata and controls
58 lines (49 loc) · 909 Bytes
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
#include "queue.h"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void initq(queue *q, int length) {
q->size = length;
q->arr = (char*) malloc(length * sizeof(int));
if (q->arr == NULL)
return ;
q->front = -1;
q->rear = -1;
q->c=0;
}
void deletequeue(queue *q) {
q->front = -1;
q->rear = -1;
q->size = 0;
q->c=0;
free(q->arr);
return;
}
void enqueue(queue *q, char element) {
if (isfullq(q))
return ;
if (q->front == -1)
q->front = 0;
q->rear = (q->rear + 1) % q->size;
q->arr[q->rear] = element;
q->c++;
return ;
}
char dequeue(queue *q) {
char d;
d = q->arr[q->front];
if (q->front == q->rear) {
q->front = -1;
q->rear = -1;
return d;
}
q->front = (q->front + 1) % q->size;
q->c--;
return d;
}
int isfullq(queue *q) {
return (q->c==q->size);
}
int isemptyq(queue *q) {
return (q->c==0);
}