-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayQueue.java
More file actions
77 lines (67 loc) · 1.5 KB
/
ArrayQueue.java
File metadata and controls
77 lines (67 loc) · 1.5 KB
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
package src;
/**
* This class is for completion in exerise 1C part ii
*/
public class ArrayQueue<E> implements QueueADT<E>
{
E[] Q;
int f,r;
int N; // array size
/**
* Constructor for objects of class ArrayQueue.
* In assignment 1C part ii no change need be made to this constructor.
* With this constructor a queue can hold up to 3 (i.e. N-1)items when
* using the approach described in lectures.
*/
public ArrayQueue()
{
N=4;
Q = (E[]) new Object[N];
f=0;
r=0;
}
// In exercise 1C part ii complete the methods below using an array implementation
/**
@throws FullQueueException
*/
public void enqueue(E element){
if (size() == N-1 ){
throw new FullQueueException("The stack is full, cannot queue");
}
else{
Q[r] = element;
r = (r + 1)%N;
}
}
/**
@throws EmptyQueueException
*/
public E dequeue(){
E e;
if (isEmpty()){
throw new EmptyQueueException("Stack is empty, cannot read from stack");
}
else{
e = Q[f];
f = (f + 1)%N;
}
return e;
}
/**
@throws EmptyQueueException
*/
public E front(){
if(isEmpty()){
throw new EmptyQueueException("Stack is empty, cannot read from stack");
}
else{
return Q[f];
}
}
public int size(){
return (N-f+r)%N;
}
public boolean isEmpty(){
return (f==r);
}
}