-
Notifications
You must be signed in to change notification settings - Fork 4
/
QueueUsingArray.java
75 lines (64 loc) · 1.42 KB
/
QueueUsingArray.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package queue.array;
import lombok.Getter;
public class QueueUsingArray {
@Getter
private int capacity;
@Getter
private int size;
private int front=-1,rear=-1;
private Integer q[];
public QueueUsingArray(int capacity) {
this.capacity = capacity;
q = new Integer[capacity];
}
public void insert(Integer elt) {
if(isFull()) {
throw new RuntimeException("Queue is full.");
} else {
if(isEmpty()) {
front = (front+1) % capacity;
}
rear = (rear+1) % capacity;
q[rear] = elt;
size++;
}
}
public Integer delete() {
if(isEmpty()) {
throw new RuntimeException("Queue is empty.");
} else {
Integer frontItem = getFrontItem(); // Fetching item present at front, This is the item to be deleted.
if(size==1) {
rear=-1;
} else {
front = (front+1) % capacity;
}
size--;
return frontItem;
}
}
public Integer getFrontItem() {
if(!isEmpty()) return q[front];
return null;
}
public Integer getRearItem() {
if(!isEmpty()) return q[rear];
return null;
}
public boolean isFull() {
if(size==capacity) return true;
return false;
}
public boolean isEmpty() {
if(size==0 && front<0 && rear<0) return true;
return false;
}
@Override
public String toString() {
String res="";
for(int i=0, j=front; i<size; j=(j+1)%capacity, i++) {
res = res + q[j]+" ";
}
return res;
}
}