Section: The Singly-Linked Structure
Use the private inner node class below to help answer the questions that follow
1. What is the value of first.data? ______
2. What is the value of first.next.next.data? ______
3. What is the value of first.next.data? ___
4. What is the value of first.next.next.next? ______
5. Write the code that will generate the following linked structure.
6. Draw a picture of the linked structure built from this code:
Node n1 = new Node("aaa");
n1.next = new Node("bbb");
n1.next.next = new Node("ccc");
n1.next.next.data = "fff";
7. Using the code in question 6, What happens with this code n1.next.next.next.data = "ggg";
8. Code Demo: GenericLinkedBag<E> implements GenericBag<E>
publicinterface GenericBag<E> {
publicboolean isEmpty(); // Comments removed to save space
publicvoid add(E element);
publicint occurencesOf(E element);
publicboolean remove(E element);
}
publicclass GenericLinkedBag<E> implements GenericBag<E> {
privateclass Node {
private E data; // Reference to the element
private Node next; // null, or reference to next node
public Node(E element) { // Different constructor
data = element;
next = null;
}
public Node(E element, Node nextRef) { // Different constructor
data = element;
next = nextRef;
}
}
// An external reference to the first element
private Node first;
// Construct an empty bag
public GenericLinkedBag() {
first = null;
}
// Add element to this Bag even if element equals another element
// already in this Bag. New elements may be added anywhere.
publicvoid add(E element) {
}
// Return true if this Bag has no elements.
publicboolean isEmpty() {
returnfalse;
}
// Return how frequently element exists in this Bag.
// Precondition: element overrides equals to compare state
publicint occurencesOf(E element) {
return -1;
}
// Remove an element from this Bag if it equals the argument and return true.
// If element is not found, return false and make no changes made to this Bag.
// Precondition: element overrides equals to compare state.
publicboolean remove(E element) {
returnfalse;
}
}
/**
* This unit test shows a further specification of the Bag ADT. It can also be
* used later to develop ArrayBag and test that the new class works correctly.
*/
importstatic org.junit.Assert.*;
import org.junit.Test;
publicclass GenericLinkedBagTest {
@Test
publicvoid testOccurencesOfWhenMoreThanOneExists() {
GenericBag<Integer> names = new GenericLinkedBag<Integer>();
names.add(99);
names.add(44);
names.add(99);
names.add(99);
assertEquals(0, names.occurencesOf(-123));
assertEquals(1, names.occurencesOf(44));
assertEquals(3, names.occurencesOf(99));
}
@Test
publicvoid testRemove() {
GenericBag<String> names = new GenericLinkedBag<String>();
names.add("Sam");
names.add("Chris");
names.add("Devon");
assertTrue(names.remove("Sam"));
assertTrue(names.remove("Chris"));
assertTrue(names.remove("Devon"));
assertEquals(0, names.occurencesOf("Sam"));
assertEquals(0, names.occurencesOf("Chris"));
assertEquals(0, names.occurencesOf("Devon"));
assertTrue(names.isEmpty());
}
}