X370: LinkedBagEquals

For this question assume the following code:


public class LinkedBag<T> implements BagInterface<T>{
    private Node firstNode;
    private int numberOfEntries;

    public LinkedBag() {
      this.firstNode = null;
      this.numberOfEntries = 0;
    }

    public LinkedBag() {
       this.firstNode = null;
       this.numberOfEntries = 0;
    }
    public int getCurrentSize(){
       return numberOfEntries;
    }
}


public static class  Node<T> {
    private T data;
    private Node<T> next;
    public Node(T dataPortion, Node<T> nextNode) {
        data = dataPortion;
        next = nextNode;
    } // end constructor
    public Node(T dataPortion) {
        this(dataPortion, null);
    } // end constructor
    public Node<T> getNext() {
        return next;
    }
    public void setNext(Node<T> newNext) {
        next = newNext;
    }
    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }
}

Given the following method signature, write a isSame method that will return true if two LinkedBags contain identical information

For example, if:

  • linkedbag1 contains "A" - "B" - "C"
  • linkedbag2 contains "A" - "B" - "C"
  • linkedbag3 contains "A" - "D" - "B"

Then

linkedbag1.isSame(linkedbag2); //returns true
linkedbag1.isSame(linkedbag3); //returns false

You will not have access to any of the methods defined in BagInterface

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.