X463: LinkedBag contains

Write a LinkedBag<T> member method called contains() that takes an element as a parameter and returns true if the bag contains the given element.

Your contains() method implementation will be inserted/compiled inside the LinkedBag<T> code seen here

public class LinkedBag<T> implements BagInterface<T> {
    private Node firstNode; // Reference to first node
    private int numberOfEntries;
    public LinkedBag() {
        firstNode = null;
        numberOfEntries = 0;
    }
}

You will NOT have access to any of the member methods in the bag interface (add, remove, clear, etc.) Recall that the LinkedBag<T> class has this nested Node class, and your implementation may directly access a Node's data and next fields.

private class Node {
    private T data; // Entry in bag
    private Node next; // Link to next node

    private Node(T dataPortion) {
        this(dataPortion, null);
    } // end constructor


    private Node(T dataPortion, Node nextNode) {
        data = dataPortion;
        next = nextNode;
    } // end constructor
} // end Node

Write your implementation of contains() below.

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.