-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListSolutionQueue
More file actions
80 lines (69 loc) · 2.17 KB
/
ArrayListSolutionQueue
File metadata and controls
80 lines (69 loc) · 2.17 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
import java.util.ArrayList;
/**
* The class <b>ArrayListSolutionQueue</b>
* is an implementation of the interface
* <b>SolutionQueue</b> which relies on
* an instance of <b>ArrayList<Solution></b>
* to store the elements of the queue.
*
* @author Ta Anh Minh, 300078762, University Of Ottawa
*/
public class ArrayListSolutionQueue implements SolutionQueue {
/**
* <b>queue</b> stores the references of the elements
* currentluy in the queue
*/
private ArrayList<Solution> queue;
/**
* Constructor, initializes <b>queue</b>
*/
public ArrayListSolutionQueue() {
queue = new ArrayList<Solution> ();
}
/**
* implementation of the method <b>enqueue</b>
* from the interface <b>SolutionQueue</b>.
* @param value
* The reference to the new element
*/
public void enqueue(Solution value) {
if (value == null) {
System.out.println("The entered value is not acceptable, please check the code");
} else {
queue.add(value); // adding a solution into the list
for (int i = 0 ; i < queue.size() -1; i++) {
Solution temp = queue.get(queue.size() - i- 1);
queue.set(queue.size() - i - 1, queue.get(queue.size() - i- 2));
queue.set(queue.size() - i - 2, temp); // putting the element added to the front of the list (tail)
}
}
}
/**
* implementation of the method <b>dequeue</b>
* from the interface <b>SolutionQueue</b>.
* @return
* The reference to removed Solution
*/
public Solution dequeue() {
if (queue.size() > 0) { // if the queue is empty
Solution remove = queue.get(queue.size()-1);
queue.remove(queue.size()-1); //remove the element at the head
return remove;
} else {
System.out.println("There is nothing to remove in the queue");
return null;
}
}
/**
* implementation of the method <b>isEmpty</b>
* from the interface <b>SolutionQueue</b>.
* @return
* true if the queue is empty
*/
public boolean isEmpty() {
if (queue.size() == 0) {
return true;
}
return false;
}
}