-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_que.c
More file actions
65 lines (65 loc) · 1.07 KB
/
circular_que.c
File metadata and controls
65 lines (65 loc) · 1.07 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
// abstract data typre of queue
// queue is empty -front is equal to rear
#include <stdio.h>
#include <stdlib.h>
struct queue
{
int size;
int front;
int rear;
int *Q;
};
void create(struct queue *q, int size1)
{
q->size = size1;
q->front = q->rear = -1;
q->Q = (int *)malloc(q->size * sizeof(int));
}
void enqueue(struct queue *q, int x)
{
if ((q->rear +1)%q->size==q->front)
{
printf("queue is full");
}
else
{
q->rear=(q->rear+1)%q->size;
q->Q[q->rear] = x;
}
}
int dequeue(struct queue *q)
{
int x = -1;
if (q->rear == q->front)
{
printf("queue is empty\n");
}
else
{
q->front++;
x = q->Q[q->front];
}
return x;
}
void display(struct queue q)
{
int i;
for ( i = q.front + 1; i <= q.rear; i++)
{
printf("%d", q.Q[i]);
printf("\n");
}
}
int main()
{
struct queue q;
create(&q, 5);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
display(q);
dequeue(&q);
printf("\n");
display(q);
return 0;
}