-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.java
50 lines (37 loc) · 982 Bytes
/
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
// This class implements the Queue
public class Queue<V> {
public Object[] queue;
public int capacity, currentSize=0, front=0, rear=0;
public Queue(int capacity) {
this.capacity=capacity;
queue=new Object[capacity];
}
public int size() {
return currentSize;
}
public boolean isEmpty() {
return size()==0;
}
public boolean isFull() {
return size()==capacity;
}
public void enqueue(Object node) {
if(isFull()) {
//System.out.println("Queue is full");
}
else {
queue[rear] = node;
rear=(rear+1)%capacity;
currentSize=currentSize + 1;}
}
public V dequeue() {
if(isEmpty()) {
return null;
}
else {
Object noderemoved=queue[front];
front=(front+1)%capacity;
currentSize=currentSize - 1;
return (V) noderemoved;}
}
}