-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendArray.java
More file actions
78 lines (69 loc) · 1.66 KB
/
ExtendArray.java
File metadata and controls
78 lines (69 loc) · 1.66 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
78
package src;
public class ExtendArray<E> implements QueueADT<E> {
E[] Q;
E[] M;
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 ExtendArray()
{
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() == Q.length - 1){
int L = 2*N;
M = (E[]) new Object[L];
for(int i = 0; i < Q.length; i++){
M[i] = Q[i];
f = (f+1)%Q.length;
Q = M;
}
}
Q[r] = element;
r = (r + 1)%Q.length;
System.out.println(size());
}
/**
@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)%Q.length;
}
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 (Q.length-f+r)%Q.length;
}
public boolean isEmpty(){
return (f==r);
}
}