-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
40 lines (35 loc) · 803 Bytes
/
Copy pathQueue.java
File metadata and controls
40 lines (35 loc) · 803 Bytes
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
public class Queue
{
int n;
int j;
Object a[];
public Queue(){
n=0;
j=0; //pointer to mark beginning of the queue
a= new Object[10];
}
public void enqueue(Object x ){
if(n==a.length){
resize();
}
a[(j+n) % a.length] = x; //circular implementation O(1)
n++;
}
public Object dequeue(){
Object x= a[j];
j=(j+1) % a.length;
n=n-1;
if(n<=a.length/3){
resize();
}
return x;
}
public void resize(){
Object b[]=new Object[2*n];
for (int i=0; i<n; i++){ //loop n times copy elemnts in right position O(n)
b[i]=a[(j+i)%a.length];
}
j=0;
a=b;
}
}