-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.java
53 lines (42 loc) · 1.09 KB
/
Queue.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
// This class implements the Queue
public class Queue<V> implements QueueInterface<V>{
//TODO Complete the Queue implementation
private NodeBase<V>[] queue;
private int capacity, currentSize, front, rear;
@SuppressWarnings("unchecked")
public Queue(int capacity) {
queue=new NodeBase[capacity];
front=rear=-1;
currentSize=0;
this.capacity=capacity;
}
public int size() {
return currentSize;
}
public boolean isEmpty() {
return currentSize<=0;
}
public boolean isFull() {
return currentSize>=capacity;
}
public void enqueue(Node<V> node) {
if (isFull())
return;
if(front==-1)
front=0;
rear=(rear+1)%capacity;
queue[rear]=node;
currentSize++;
}
public NodeBase<V> dequeue() {
if (isEmpty())
return null;
NodeBase<V> R = queue[front];
if (front==rear)
front=rear=-1;
else
front=(front+1)%capacity;
currentSize--;
return R;
}
}