-
Notifications
You must be signed in to change notification settings - Fork 0
/
circular-queue-arr.c
73 lines (58 loc) · 1.12 KB
/
circular-queue-arr.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
65
66
67
68
69
70
71
72
73
//cir-queue-arr.c
#include <stdio.h>
#define SIZE 5
int queue[SIZE];
int front=-1,rear=-1;
int isEmpty() {
if(front==-1) return 1;
else return 0;
}
int isFull() {
if( (rear+1)%SIZE==front) return 1;
else return 0;
}
int atFront() {
return queue[front];
}
void insert(int element) {
if(isFull())
printf("Queue is full\n");
else {
if(rear==-1)
front=rear=0;
else
rear=(rear+1)%SIZE;
queue[rear]=element;
}
}
void del() {
if(isEmpty()==1)
printf("Queue is empty\n");
else
{
if(front==rear)
front=rear=-1;
else
front=(front+1)%SIZE;
}
}
main()
{
insert(10); insert(11); insert(12); insert(13); insert(14);
insert(50); // should get queue full message
if(!isEmpty())
printf("At front we have %d\n",atFront());
del(); del();
if(!isEmpty())
printf("At front we have %d\n",atFront());
insert(50);
insert(60);
del(); del(); del();
if(!isEmpty())
printf("At front we have %d\n",atFront());
del();
if(!isEmpty())
printf("At front we have %d\n",atFront());
del();
del(); // should get queue empty message
}