Spaces:
Running
Running
this.queue.add(0, element);
Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/05. Generic Queue Class
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
In the tutorial, a generic class Stack was introduced.
|
| 2 |
+
|
| 3 |
+
Queue is a data structure similar to a stack.
|
| 4 |
+
The difference is that while in a stack, elements are added and removed from the same end, => LIFO
|
| 5 |
+
in a queue, 'elements are added to the end but removed from the beginning' (just like how a queue works in real life). => FIFO
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
Write a generic class Queue<T> with the following properties:
|
| 9 |
+
A constructor that does not take any parameters.
|
| 10 |
+
A method void add(T element) that adds an element to the front of the queue. => counterintuitive; add to the front
|
| 11 |
+
A method T remove(), which removes and returns the last element of the queue. => counterintuitive; but remove from the back
|
| 12 |
+
A method boolean hasElements(), which returns true if there is at least one element in the queue.
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
import java.util.Random;
|
| 18 |
+
import java.util.ArrayList;
|
| 19 |
+
|
| 20 |
+
public class Test {
|
| 21 |
+
public static void main(String[] args) {
|
| 22 |
+
final Random r = new Random();
|
| 23 |
+
|
| 24 |
+
System.out.println("Testing the Queue class...");
|
| 25 |
+
|
| 26 |
+
System.out.println("Testing with integer type...");
|
| 27 |
+
Queue<Integer> queue = new Queue<>();
|
| 28 |
+
int[] numbers = {2, 4, 6, 3, 5};
|
| 29 |
+
|
| 30 |
+
for (int number : numbers) {
|
| 31 |
+
System.out.println("Adding element " + number);
|
| 32 |
+
queue.add(number);
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
System.out.println("Removing elements until hasElements == false");
|
| 36 |
+
while (queue.hasElements()) {
|
| 37 |
+
System.out.println(queue.remove());
|
| 38 |
+
}
|
| 39 |
+
System.out.println("");
|
| 40 |
+
|
| 41 |
+
System.out.println("Testing with string type...");
|
| 42 |
+
Queue<String> queue2 = new Queue<>();
|
| 43 |
+
String[] elements = {"rabbit", "fox", "bear", "elk"};
|
| 44 |
+
|
| 45 |
+
for (String element : elements) {
|
| 46 |
+
System.out.println("Adding element " + element);
|
| 47 |
+
queue2.add(element);
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
System.out.println("Removing elements until hasElements == false");
|
| 51 |
+
while (queue2.hasElements()) {
|
| 52 |
+
System.out.println(queue2.remove());
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
//ADD
|
| 61 |
+
class Queue<T> {
|
| 62 |
+
// attributes
|
| 63 |
+
private ArrayList<T> queue;
|
| 64 |
+
|
| 65 |
+
// constructor with no params
|
| 66 |
+
public Queue(){
|
| 67 |
+
this.queue = new ArrayList<>();
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
// normal ArrayList
|
| 71 |
+
// https://stackoverflow.com/questions/12949690/java-arraylist-how-to-add-elements-at-the-beginning
|
| 72 |
+
// do not use LinkedList
|
| 73 |
+
// LinkedList example from Oracle
|
| 74 |
+
// https://docs.oracle.com/javase/7/docs/api/java/util/Deque.html#addFirst(E)
|
| 75 |
+
// add element to the front
|
| 76 |
+
public void add(T element) {
|
| 77 |
+
// for LinkedList
|
| 78 |
+
// this.queue.addFirst(element);
|
| 79 |
+
// for ArrayList
|
| 80 |
+
this.queue.add(0, element);
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
// remove and return last element
|
| 84 |
+
public T remove() {
|
| 85 |
+
return this.queue.remove(this.queue.size() - 1);
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
public boolean hasElements() {
|
| 89 |
+
return this.queue.size() > 0;
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
Testing the Queue class...
|
| 98 |
+
Testing with integer type...
|
| 99 |
+
Adding element 2
|
| 100 |
+
Adding element 4
|
| 101 |
+
Adding element 6
|
| 102 |
+
Adding element 3
|
| 103 |
+
Adding element 5
|
| 104 |
+
Removing elements until hasElements == false
|
| 105 |
+
2
|
| 106 |
+
4
|
| 107 |
+
6
|
| 108 |
+
3
|
| 109 |
+
5
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
Testing with string type...
|
| 114 |
+
Adding element rabbit
|
| 115 |
+
Adding element fox
|
| 116 |
+
Adding element bear
|
| 117 |
+
Adding element elk
|
| 118 |
+
Removing elements until hasElements == false
|
| 119 |
+
rabbit
|
| 120 |
+
fox
|
| 121 |
+
bear
|
| 122 |
+
elk
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|