-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
64 lines (49 loc) · 1.08 KB
/
Queue.java
File metadata and controls
64 lines (49 loc) · 1.08 KB
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
// 6
public class Queue {
Object a [];
int n;
int j;
public Queue () {
a = new Object[10];
int n = 0;
int j = 0;
}
public void enque (Object x) {
if (n == a.length){
resize();
}
a[(j + n) % a.length] = x;
}
public Object dequeue (){
Object x = a[j];
j = (j + 1) % a.length;
n --;
if (n <= a.length/3){
resize();
}
return x;
}
public void resize(){
Object [] b = new Object [2*n];
for (int i = 0 ; i < n ; i ++){
b[i] = a[(j+n)% a.length];
}
j = 0;
a = b;
}
public String toString(){
String output = "";
for(int i=0; i<n; i++){
output = output + a[(j + i)% a.length] + " ";
}
return output;
}
public static void main(String[] args) {
Queue q = new Queue();
q.enque("Celine");
q.enque("Jungkook");
q.enque("Taehyung");
// System.out.println(q.dequeue());
System.out.println(q);
}
}