simplest
simplest

Reputation: 217

Java comparing a created Linked List

I am going over the basics of creating a Linked List and need to find out if these two are equal before and after a replace. The replace is working properly as I have print statements in my main method to check, but I am somehow not correctly comparing these two after the replace. The problem is in my equals method.

    public boolean replace(int index, Object newEntry) {
        boolean isSuccessful = true;
        Node nodeToReplace = getNode(index);
        getNode(index).data = newEntry;

        if(newEntry == nodeToReplace){
            isSuccessful = false;
        }
        return isSuccessful;
    } // end replace
    /** Task: Determines whether two lists are equal.
     * @param other object that contains the other linked list
     * @return true if two lists have the same length and all
     * entries are equal, or false if not */
    public boolean equals(Object other) {
        MyLinkedList aList = (MyLinkedList)other;
        boolean isEqual = true; // result of comparison of lists
        Node currentNode = firstNode;
        Node aListNode = firstNode;

        while ((currentNode != null)){
            if (currentNode.data == aListNode.data) {
                currentNode = currentNode.next;
                aListNode = aListNode.next;
            }
            else
                isEqual = false;
        }
        return isEqual;
    } // end equals
    public int getLength() {
        return length;
    }
    public boolean isEmpty() {
        return length == 0;
    }

    // @return an string with all entries in the list
    public String toString() {
        Node currentNode = firstNode;
        String s = new String();
        while (currentNode != null) {
            s += currentNode.data + " ";
            currentNode = currentNode.next;
        }
        return s;
    }
    private Node getNode(int index) {
        Node currentNode = firstNode;
        // traverse the list to locate the desired node
        for (int counter = 0; counter < index; counter++)
            currentNode = currentNode.next;
        return currentNode;
    } // end getNode
    private class Node {
        private Object data; // data portion
        private Node next; // link to next node

        private Node(Object dataPortion) {
            data = dataPortion;
            next = null;
        } // end constructor
        private Node(Object dataPortion, Node nextNode) {
            data = dataPortion;
            next = nextNode;
        } // end constructor
        private void setData(Object dataPortion) {
            data = dataPortion;
        } // end setData
        private Object getData() {
            return data;
        } // end getData
        private void setNextNode(Node nextNode) {
            next = nextNode;
        } // end setNextNode
        private Node getNextNode() {
            return next;
        } // end getNextNode
    } // end Node

Upvotes: 0

Views: 70

Answers (2)

Quik19
Quik19

Reputation: 362

In you equals function, you're starting both currentNode and aListNode on the same place.

I believe you want to initialize currentNode to the old list and aListNode to the new one, or vice-versa in order to compare them both, otherwise you'll always get a true return from the function

Upvotes: 1

Willis Blackburn
Willis Blackburn

Reputation: 8204

Is it always saying they're equal?

You're initializing currentNode and aListNode with the same node:

Node currentNode = firstNode;
Node aListNode = firstNode;

You probably want this:

Node currentNode = firstNode;
Node aListNode = aList.firstNode;

Once you fix that, you'll find it runs forever. You should return false once you realize the two lists are not equal. Then you can get rid of isEqual. Right now you set isEqual to false, but you never leave the loop.

Upvotes: 2

Related Questions