0

I'm working on a project that appends nodes to the end of a linked list. However, I cannot add the same node twice inside a list and when I append the same node as before the nodes increase naturally and I don't get a message saying the node already exists. Here's the code

public int append(int item) {    
    ItemNode node = new ItemNode(item);    
    if (this.empty()) {
        first = node;
        last = node;
        nbNodes++;
    } else if (node == findNode(item)) {
        System.out.println("Node already exists");
    } else {
        last.next = node;
        last = node;
        nbNodes++;    
    }
    return nbNodes;
}

private ItemNode findNode(int key) {    
    if (this.empty()) {
        return null;
    } else {    
        ItemNode current = this.first;    
        while (current.item != key) {
            if (current.next == null) {
                return null;
            } else {
                current = current.next;
            }
        }
        return current;
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • `node==findNode(item)` - that's wrong. You should use `equals()` and override `equals()` in your `ItemNode` class. – Eran Dec 01 '19 at 12:34
  • Just check if it's not null, if(findNoe(item) != null) { ...} – b.GHILAS Dec 01 '19 at 12:36
  • I'm not trying to check only if it's null I'm trying to check if a node already exists –  Dec 01 '19 at 12:45
  • This may help you - https://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator – Eran Dec 01 '19 at 13:02
  • I found it earlier –  Dec 01 '19 at 13:06
  • @user12437216 that's exactly what I'm saying, if findNode(item) != null then that means the node already exists – b.GHILAS Dec 01 '19 at 13:10
  • You are juggling quite many questions around your custom implementation of LinkedList: [Remove Node](https://stackoverflow.com/questions/59090684/java-remove-node-from-simply-linked-list), [Append Node](https://stackoverflow.com/questions/59126305/java-add-elements-in-linked-list-incrementally), [Contains Sublist](https://stackoverflow.com/questions/59049534/how-to-check-if-a-list-is-a-subset-of-another-list-in-java). Consider __cross-linking them__, so we can build your __puzzle to working LinkedList__ implementation! – hc_dev Dec 01 '19 at 17:51

1 Answers1

0

As Eran already mentioned, you should use equals by overriding it as per your needs. The reason you are not getting the message is that you are comparing the references of newly created object.

ItemNode node = new ItemNode(item); 

with that of an existing one i.e

node == findNode(item).// Here findNode will return an existing node, so they are not pointing to same object

Possible Solutions :

1) You can make your findNode(int key) return a boolean type. This way if a node is found with the given key you can return true or false.

2) Or you can compare the items for both i.e

else if(node.item == findNode(item).item) // assuming item is of primitive int type

Hope it helps !

Shubham
  • 549
  • 4
  • 11