-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.c
64 lines (55 loc) · 879 Bytes
/
queue.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
#include<stdio.h>
#include<stdlib.h>
#define s 10
struct queue
{
int *data, size;
int rear, front;
};
int dequeue(struct queue *);
void enqueue(struct queue *,int );
int isEmpty(struct queue *);
int isFull(struct queue *);
int isEmpty(struct queue *q)
{
if(q->rear == q->front)
return 1;
return 0;
}
int isFull(struct queue *q)
{
if(q->front == q->rear+1)
return 1;
return 0;
}
void enqueue(struct queue *q,int x)
{
if(!isFull(q))
{
q->rear += 1;
q->data[q->rear] = x;
}
}
int dequeue(struct queue *q)
{
if(isEmpty(q))
{
printf("Underflow");
return 0;
}
free(q->data[q->front]);
q->front += 1;
}
int main()
{
struct queue *q = malloc(sizeof(struct queue));
q->size = s;
q->rear = q->front = -1;
q->data = malloc(q->size * sizeof(int));
enqueue(q,1);
enqueue(q,2);
dequeue(q);
dequeue(q);
printf("%d", q->data[q->front]);
return 0;
}