-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubleended.c
100 lines (85 loc) · 1.46 KB
/
doubleended.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//doubleended.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];
}
int atRear() {
return queue[rear];
}
void insertRear(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 insertFront(int element) {
if(isFull())
printf("Queue is full\n");
else {
if(front==-1)
front=rear=0;
else
{
front=(front-1)%SIZE;
if(front<0) front=SIZE-1;
}
queue[front]=element;
}
}
void delFront() {
if(isEmpty()==1)
printf("Queue is empty\n");
else
{
if(front==rear)
front=rear=-1;
else
front=(front+1)%SIZE;
}
}
void delRear() {
if(isEmpty()==1)
printf("Queue is empty\n");
else
{
if(front==rear)
front=rear=-1;
else
{
rear=(rear-1)%SIZE;
if(rear<0) rear=SIZE-1;
}
}
}
main()
{
insertRear(11);
insertRear(12);
insertRear(13);
insertRear(14);
insertRear(15);
printf("At front: %d, At Rear: %d\n",atFront(),atRear());
delRear();
insertFront(100);
insertFront(200);
printf("At front: %d, At Rear: %d\n",atFront(),atRear());
delRear();
insertFront(300);
printf("At front: %d, At Rear: %d\n",atFront(),atRear());
}