-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayQueueCirc.java
52 lines (50 loc) · 1011 Bytes
/
ArrayQueueCirc.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
public class ArrayQueueCirc <T> implements QueueInterface <T> {
private T[] queue;
private int tailIndex;
private int headIndex;
public static final int DEF_SIZE = 50;
public ArrayQueueCirc()
{
init(DEF_SIZE);
}
public ArrayQueueCirc(int size)
{
init(size);
}
public void init(int size)
{
if(size <= 0)
{
return;
}
tailIndex = headIndex = 0;
queue = (T[])(new Object[size]);
}
public void enqueue(T aData)
{
if(queue[headIndex] != null && headIndex == tailIndex)
{
return;
}
queue[tailIndex] = aData;
tailIndex = (tailIndex + 1) % queue.length;
}
public T dequeue()
{
T ret = queue[headIndex];
headIndex = (headIndex + 1) % queue.length;
return ret;
}
public T peek()
{
return queue[headIndex];
}
public void print()
{
System.out.println(queue[headIndex]);
for(int i = (headIndex + 1) % queue.length; i != tailIndex; i = (i + 1) % queue.length)
{
System.out.println(queue[i]);
}
}
}