Question
Giving Code: public final class LinkedBag implements BagInterface { private Node firstNode; // Reference to first node private int numberOfEntries; public LinkedBag() { firstNode =
Giving Code:
public final class LinkedBag
private Node firstNode; // Reference to first node private int numberOfEntries;
public LinkedBag() { firstNode = null; numberOfEntries = 0; } // end default constructor
/** * Gets the number of entries currently in this bag. * * @return The integer number of entries currently in this bag. */ @Override public int getCurrentSize() { return numberOfEntries; } // end getCurrentSize
/** * Sees whether this bag is empty. * * @return True if this bag is empty, or false if not. */ @Override public boolean isEmpty() { return numberOfEntries == 0; } // end isEmpty
/** * Adds a new entry to this bag. * * @param newEntry The object to be added as a new entry * @return True if the addition is successful, or false if not. */ @Override public boolean add(T newEntry) // OutOfMemoryError possible { // Add to beginning of chain: Node newNode = new Node(newEntry); newNode.next = firstNode; // Make new node reference rest of chain // (firstNode is null if chain is empty) firstNode = newNode; // New node is at beginning of chain numberOfEntries++;
return true; } // end add
/** * Retrieves all entries that are in this bag. * * @return A newly allocated array of all the entries in this bag. */ @Override public T[] toArray() { // The cast is safe because the new array contains null entries @SuppressWarnings("unchecked") T[] result = (T[]) new Object[numberOfEntries]; // Unchecked cast
int index = 0; Node currentNode = firstNode; while ((index
return result; } // end toArray
// Locates a given entry within this bag. // Returns a reference to the node containing the entry, if located, // or null otherwise. private Node getReferenceTo(T anEntry) { boolean found = false; Node currentNode = firstNode;
while (!found && (currentNode != null)) { if (anEntry.equals(currentNode.data)) { found = true; } else { currentNode = currentNode.next; } } // end while
return currentNode; } // end getReferenceTo
/** * Removes all occurrences of the given entries * */ @Override public void removeAllOccurences(T[][] entries) {
// Convert 2D array to 1D array
// Remove duplicates in array
// Remove all occurences of given items } // end removeAllOccurences
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 } // end LinkedBag
please Help!!
PART A-The Bag, 20 points Define the method removeAlloccurrences for the class LinkedBag that removes all occurrences of the given entries from a bag. Your program's output must be identical to this output Creating an empty bag - The bag now contains 0 string (s) + Creating bag items... - The bag now contains 18 string (s): Adding the bag items to the bag A Creating a 2D testArray B Bb Bb n u l Removing testArray items from the bag. -- - The bag now contains 4 string (s) G oo d PART B - The Efficiency of Algorithms, 10 points 1. Show how you count the number of operations (not only 2. Consider the following two loops, 4 points basic operations) required by the algorithm, 4 points: //Loop for (i-1; iStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started