-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayQueue.java
91 lines (78 loc) · 1.79 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//Benjamin Maymir
//maymi001
// ARRAY QUEUE. A fixed length queue represented as a circular array.
class ArrayQueue<Base>
{
private int front; // Index of front object in OBJECTS.
private int rear; // Index of rear object in OBJECTS.
public Base[] objects; // The OBJECTs in the queue.
// Constuctor. Make a new empty queue that can hold SIZE - 1 elements.
public ArrayQueue(int size)
{
if (size <= 1)
{
throw new IllegalArgumentException("Illegal size.");
}
else
{
front = 0;
rear = 0;
objects = (Base []) new Object[size];
}
}
// DEQUEUE. Remove an object from the queue.
public Base dequeue()
{
if (front == rear)
{
throw new IllegalStateException("Queue is empty.");
}
else
{
front = (front + 1) % objects.length;
Base temp = objects[front];
objects[front] = null;
return temp;
}
}
// ENQUEUE. Add a new OBJECT to the queue.
public void enqueue(Base object)
{
int nextRear = (rear + 1) % objects.length;
if (front == nextRear)
{
throw new IllegalStateException("Queue is full.");
}
else
{
rear = nextRear;
objects[rear] = object;
}
}
// IS EMPTY. Test if the queue is empty.
public boolean isEmpty()
{
return front == rear;
}
// IS FULL. Test if the queue is full.
public boolean isFull()
{
return front == (rear + 1) % objects.length;
}
public Iterator iterator(){
return new Iterator();
}
public class Iterator{
private int position;
private Iterator(){
position=front;
}
public boolean hasNext(){
return (position)%objects.length!=rear;
}
public Base next(){
position=(position+1)%objects.length;
return objects[(position)];
}
}
}