- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayQueue.java
More file actions
Latest commit
117 lines (103 loc) · 3.14 KB
/
Copy pathArrayQueue.java
File metadata and controls
117 lines (103 loc) · 3.14 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
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
110
111
112
113
114
115
116
117
packageorg.cct;
importjava.util.NoSuchElementException;
/**
* A Queue is a data structure that stores data (integers here) according to a set of rules.
* Specifically each element added to the queue gets added to the end (tail) of the Queue, and every
* element removed from the Queue is taken from the front of the queue (head).
*
* @author Jenny
* @version 0.0.1
*/
publicclassArrayQueue {
intsize = 0; // current number of elements in the Queue
int[] arrayQueue; // data structure to hold the elements
intmaxCapacity; // maximum capacity of the queue
inthead; // pointer to the front of the Queue
inttail; // pointer to the end of the Queue
/**
* Default constructor to set default capacity to 10
*/
publicArrayQueue() {
maxCapacity = 10;
arrayQueue = newint[maxCapacity];
//Fix 3: We are not moving head anymore so it`s always 0
head = 0;
tail = -1;
size = 0;
}
/**
* Bespoke constructor that allows the client to set the maximum capacity
*
* @param maxCapacity capacity of the array holding the data
*/
publicArrayQueue(intmaxCapacity) {
this.maxCapacity = maxCapacity;
arrayQueue = newint[maxCapacity];
//Fix 3: We are not moving head anymore so it`s always 0
head = 0;
tail = -1;
size = 0;
}
/**
* Is the Queue empty or not
*
* @return whether the Queue is empty or not
*/
publicbooleanisEmpty() {
returnsize == 0;
}
/**
* Add data to the end of the queue
*
* @param data Data to be added
* @throws IndexOutOfBoundsException cannot add to a full queue (reached end of queue)
*/
publicvoidenqueue(intdata) throwsIndexOutOfBoundsException {
if (isFull()) {
thrownewIndexOutOfBoundsException("Out of bounds");
}
arrayQueue[++tail] = data;
size++;
}
/**
* Remove data from the front of the queue
*
* @return the data from the front of the queue
* @throws NoSuchElementException Cannot remove data from an empty queue
*/
publicintdequeue() throwsNoSuchElementException {
if (size == 0) {
thrownewNoSuchElementException();
}
intresult = arrayQueue[head];
//Fix 2: Take head element from the queue and shift elements towards head
System.arraycopy(arrayQueue, 1, arrayQueue, 0, size);
size--;
returnresult;
}
/**
* Determine if the Queue is full or not
*
* @return whether the Queue is full or not
*/
publicbooleanisFull() {
//Fix 1: size should equals maxCapacity
returnsize == maxCapacity;
}
/**
* Look at the data at the front of the queue, do not remove it.
*
* @return the data at the front of the queue
* @throws NoSuchElementException We cannot peek at elements in an empty queue
*/
publicintpeek() throwsNoSuchElementException {
if (size == 0) {
thrownewNoSuchElementException();
}
//Fix 4: As head is always 0 we should not increment head
returnarrayQueue[head];
}
publicintsize() {
returnsize;
}
}