For this question assume the following code:
1
2
3
public class LinkedBagT> implements BagInterfaceT>{
4
private Node firstNode;
5
private int numberOfEntries;
6
7
public LinkedBag() {
8
firstNode = null;
9
numberOfEntries = 0;
10
}
11
12
13
public int getCurrentSize(){
14
return numberOfEntries;
15
}
16
}
17
18
19
public static class NodeT> {
20
private T data;
21
private NodeT> next;
22
public Node(T dataPortion, NodeT> nextNode) {
23
data = dataPortion;
24
next = nextNode;
25
} // end constructor
26
public Node(T dataPortion) {
27
this(dataPortion, null);
28
} // end constructor
29
public NodeT> getNext() {
30
return next;
31
}
32
public void setNext(NodeT> newNext) {
33
next = newNext;
34
}
35
public T getData() {
36
return data;
37
}
38
public void setData(T data) {
39
this.data = data;
40
}
41
}
Recall that a set is a datastructure that only stores unique values.
Given the following method signature, write an add method that would only add unique values to the LinkedBag. If a duplicate value would be added, your code should return false.
For example, if a LinkedBag contained "A", "B", and "C":
- Calling addLikeASet("D") would return true and add "D" to the LinkedBag.
- However, calling addLikeASet("C")would return `false` and no value would be added.
You will not have access to any of the methods defined in BagInterface