-
Notifications
You must be signed in to change notification settings - Fork 1
/
lab9.java
109 lines (89 loc) · 2.31 KB
/
lab9.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package beyen074_lab9;
import beyen074_lab9.ArrayQueue.Iterator;
public class ArrayQueue<Base> {
private int front; // Index of front object in OBJECTS.
private int rear; // Index of rear object in OBJECTS.
private Base[] objects; // The OBJECTs in the queue.
// Constuctor. Make a new empty queue that can hold SIZE - 1 elements.
@SuppressWarnings("unchecked")
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;
//front = (front + 1) % objects.length;
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 class Iterator {
//private int frontPointer; // the next element to be visited
private int nextPointer;
private int rearPointer;
private Iterator( int nextPointer, int rearPointer) {
this.nextPointer=nextPointer;
//this.frontPointer=frontPointer;
this.rearPointer=rearPointer;
}
public boolean hasNext() {
return (nextPointer != rearPointer);
}
public Base next() {
if(nextPointer ==rearPointer) {
throw new IllegalStateException("no more elements to visit");
}
else {
nextPointer = (nextPointer + 1) % objects.length;
Base temp = objects[nextPointer];
//nextPointer = (nextPointer + 1) % objects.length;
return temp;
}
}
}
public Iterator iterator() {
return new Iterator(front,rear);
}
}