-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQueue.java
76 lines (55 loc) · 1.31 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.Arrays;
public class Queue {
int size = 0;
int front = 0;
int back = -1;
Integer[] array;
public Queue(int size) {
this.size = size;
this.array = new Integer[this.size];
}
public void enqueue(int value) {
if (this.back == this.array.length - 1) {
System.out.println("Queue is full.");
return;
}
this.array[++this.back] = value;
System.out.println(Arrays.toString(array));
}
public void dequeue() {
if (this.back == -1) {
System.out.println("Queue is empty.");
return;
}
for (int i=0; i<this.array.length-1; i++) {
this.array[i] = this.array[i+1];
}
--this.back;
System.out.println(Arrays.toString(array));
}
public void peek() {
if (this.back == -1) {
System.out.println("Queue is empty.");
return;
}
System.out.println("Front element : " + this.array[this.front]);
}
public static void main(String[] args) {
Queue queue = new Queue(5);
queue.peek();
queue.enqueue(1);
queue.enqueue(2);
queue.peek();
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.peek();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
queue.enqueue(5);
queue.enqueue(6);
queue.peek();
}
}