-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayQueue.java
More file actions
57 lines (49 loc) · 1.72 KB
/
ArrayQueue.java
File metadata and controls
57 lines (49 loc) · 1.72 KB
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
import java.util.*;
public class ArrayQueue < E > {
private E[] data;
private int frontIndex;
private int queueSize;
public ArrayQueue(int capacity) {
data = (E[]) new Object[capacity];
queueSize = 0;
frontIndex = 0;
}
public ArrayQueue() {
this(1000);
}
public int size() {
return queueSize;
}
public boolean isEmpty() {
return (queueSize == 0);
}
public void enqueue(E e) throws IllegalStateException {
if (queueSize == data.length) throw new IllegalStateException("Queue is full");
int avail = (frontIndex + queueSize) % data.length;
data[avail] = e;
queueSize++;
}
public E first() throws IllegalStateException {
if (queueSize == data.length) throw new IllegalStateException("Queue is empty");
return data[frontIndex];
}
public E dequeue() throws IllegalStateException {
if (queueSize == data.length) throw new IllegalStateException("Queue is empty");
E answer = data[frontIndex];
data[frontIndex] = null;
frontIndex = (frontIndex + 1) % data.length;
queueSize--;
return answer;
}
public static void main(String[] args) {
ArrayQueue queue = new ArrayQueue();
queue.enqueue(18);
System.out.println("Element at front : " + queue.first());
System.out.println("Element removed from front : " + queue.dequeue());
System.out.println("Queue is Empty : " + queue.isEmpty());
queue.enqueue(79);
queue.enqueue(90);
System.out.println("Size of the queue : " + queue.size());
System.out.println("Element removed from front end : " + queue.dequeue());
}
}