-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleEndedQueue1.java
208 lines (178 loc) · 4.89 KB
/
DoubleEndedQueue1.java
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// All Functions of Double-Ended Queue(deque)
import java.util.*;
class deque {
int r;
int f;
int capacity;
int[] q;
deque(int size) {
capacity = size;
r = -1;
f = -1;
q = new int[capacity];
}
boolean isFull() {
return (f == r + 1) || (f == 0 && r == capacity - 1);
}
boolean isEmpty() {
return (r == -1 && f == -1);
}
void insertRear(int a) {
if (isFull()) {
System.out.println("Overflow !!");
} else {
if (f == -1) {
f++;
}
r = (r + 1) % capacity;
q[r] = a;
}
}
void insertFront(int b) {
if (isFull()) {
System.out.println("Overflow!!");
} else {
if (f == -1 && r == -1) {
f = 0;
r = 0;
} else if (f == 0) {
f = capacity - 1;
} else {
f--;
}
q[f] = b;
}
}
int deleteFront() {
if (isEmpty()) {
System.out.println("Underflow..");
return -1;
} else {
int y = q[f];
if (f == r) {
f = -1;
r = -1;
return y;
} else {
f = (f + 1) % capacity;
return y;
}
}
}
int deleteRear() {
if (isEmpty()) {
System.out.println("Underflow..");
return -1;
} else {
int y = q[r];
if (f == r) {
f = -1;
r = -1;
} else {
if (r == 0) {
r = capacity - 1;
} else {
r--;
}
}
return y;
}
}
void display() {
if (isEmpty()) {
System.out.println("Queue is Empty..");
} else {
int i = f;
while (i != r) {
System.out.println(q[i] + " ");
i = (i + 1) % capacity;
}
System.out.println(q[r]);
}
}
int getRear() {
if (r == -1) {
System.out.println("Rear is on default(-1)");
return -1;
} else {
return q[r];
}
}
int getFront() {
if (f == -1) {
System.out.println("Front is on default(-1)");
return -1;
} else {
return q[f];
}
}
void count() {
if (isEmpty()) {
System.out.println("Queue Empty..");
} else {
int count = 0;
int i = f;
while (i != r) {
count++;
i = (i + 1) % capacity;
}
System.out.println("Total elements in Queue are: " + (count + 1));
}
}
}
class Main1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of Queue: ");
int size = sc.nextInt();
deque d = new deque(size);
while (true) {
System.out.println(
"\n Press \n 1)insertRear \n 2) insertFront \n 3) deleteFront \n 4) deleteRear \n 5) display \n 6) getRear \n 7) getFront \n 8) count \n 9) isFull \n 10) isEmpty \n 11) EXIT ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter element to insert at Rear: ");
int a = sc.nextInt();
d.insertRear(a);
break;
case 2:
System.out.println("Enter element to insert at Front: ");
int b = sc.nextInt();
d.insertFront(b);
break;
case 3:
d.deleteFront();
break;
case 4:
d.deleteRear();
break;
case 5:
System.out.println("Elements in Queue are: ");
d.display();
break;
case 6:
System.out.println("Rear Element is:" + d.getRear());
break;
case 7:
System.out.println("Front Element is:" + d.getFront());
break;
case 8:
d.count();
break;
case 9:
System.out.println(d.isFull());
break;
case 10:
System.out.println(d.isEmpty());
break;
case 11:
System.exit(0);
break;
default:
System.out.println("Invalid Choice(1-11 Accepted !!)");
break;
}
}
}
}