-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathQueuesUsingArray.java
58 lines (48 loc) Β· 1.22 KB
/
QueuesUsingArray.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 Lecture16;
public class QueuesUsingArray {
private int[] data;
private int front;
private int size;
public QueuesUsingArray(int capacity) {
this.data = new int[capacity];
this.front = 0;
this.size = 0;
}
public void enqueue(int value) throws Exception {
if (this.queueSize() == this.data.length) {
throw new Exception("queue is full");
}
int position = (this.front + this.size) % this.data.length;
this.data[position] = value;
this.size++;
}
public int dequeue() throws Exception {
if (this.queueSize() == 0) {
throw new Exception("queue is empty");
}
int deqElement = this.data[this.front];
this.data[this.front] = 0;
this.size--;
this.front = (this.front + 1) % data.length;
return deqElement;
}
public int frontElement() throws Exception {
if (this.size == 0) {
throw new Exception("queue is empty");
}
return this.data[this.front];
}
public int queueSize() {
return this.size;
}
public boolean isEmpty() {
return this.queueSize() == 0;
}
public void display() {
for (int index = 0; index < this.data.length; index++) {
int elementPos = (this.front + index) % data.length;
System.out.print(this.data[elementPos] + " ");
}
System.out.println();
}
}