-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueue_basic.java
58 lines (47 loc) · 1.25 KB
/
Queue_basic.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
package Queue;
public class Queue_basic {
private int[] arr;
private int front = 0;
private int size = 0;
public Queue_basic() {
// TODO Auto-generated constructor stub
arr = new int[5];
}
public Queue_basic(int n) {
// TODO Auto-generated constructor stub
arr = new int[n];
}
public boolean isEmpty() {
return size == 0;
}
public boolean isfull() {
return size == arr.length;
}
public void Enqueue(int item) throws Exception {
if (isfull()) {
throw new Exception("Bklol Queue full h");
}
int idx = (front + size) % arr.length;
arr[idx] = item;
size++;
}
public int dequeue() throws Exception {
if (isEmpty()) {
throw new Exception("Queue khali hai");
}
int rv = arr[front];
front = (front + 1) % arr.length;
size--;
return rv;
}
public int GetFront() {
int rv = arr[front];
return rv;
}
public void Display() {
for (int i = 0; i < size; i++) {
int idx = (front + i) % arr.length;
System.out.print(arr[idx] + " ");
}
}
}