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