/
CircularQueue.java
106 lines (95 loc) · 2.91 KB
/
CircularQueue.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.thealgorithms.datastructures.queues;
// This program implements the concept of CircularQueue in Java
// Link to the concept: (https://en.wikipedia.org/wiki/Circular_buffer)
public class CircularQueue {
int[] arr;
int topOfQueue;
int beginningOfQueue;
int size;
public CircularQueue(int size) {
arr = new int[size];
topOfQueue = -1;
beginningOfQueue = -1;
this.size = size;
}
public boolean isEmpty() {
return beginningOfQueue == -1;
}
public boolean isFull() {
if (topOfQueue + 1 == beginningOfQueue) {
return true;
} else
return topOfQueue == size - 1 && beginningOfQueue == 0;
}
public void enQueue(int value) {
if (isFull()) {
System.out.println("The Queue is full!");
} else if (isEmpty()) {
beginningOfQueue = 0;
topOfQueue++;
arr[topOfQueue] = value;
System.out.println(value + " has been successfully inserted!");
} else {
if (topOfQueue + 1 == size) {
topOfQueue = 0;
} else {
topOfQueue++;
}
arr[topOfQueue] = value;
System.out.println(value + " has been successfully inserted!");
}
}
public int deQueue() {
if (isEmpty()) {
System.out.println("The Queue is Empty!");
return -1;
} else {
int res = arr[beginningOfQueue];
arr[beginningOfQueue] = Integer.MIN_VALUE;
if (beginningOfQueue == topOfQueue) {
beginningOfQueue = -1;
topOfQueue = -1;
} else if (beginningOfQueue + 1 == size) {
beginningOfQueue = 0;
} else {
beginningOfQueue++;
}
return res;
}
}
public int peek() {
if (isEmpty()) {
System.out.println("The Queue is Empty!");
return -1;
} else {
return arr[beginningOfQueue];
}
}
public void deleteQueue() {
arr = null;
System.out.println("The Queue is deleted!");
}
public static void main(String[] args) {
CircularQueue cq = new CircularQueue(5);
System.out.println(cq.isEmpty());
System.out.println(cq.isFull());
cq.enQueue(1);
cq.enQueue(2);
cq.enQueue(3);
cq.enQueue(4);
cq.enQueue(5);
System.out.println(cq.deQueue());
System.out.println(cq.deQueue());
System.out.println(cq.deQueue());
System.out.println(cq.deQueue());
System.out.println(cq.deQueue());
System.out.println(cq.isFull());
System.out.println(cq.isEmpty());
cq.enQueue(6);
cq.enQueue(7);
cq.enQueue(8);
System.out.println(cq.peek());
System.out.println(cq.peek());
cq.deleteQueue();
}
}