0

I have two lists ItemsList , ilist . If the nodes of ilist contain the same values as the nodes of ItemsList i have to delete them from ItemsList however i get that my list is empty each time i use the remove function from the code below :

 public void remove(ItemsList ilist) {
    if (empty()) {
        System.out.println("The list is empty.");
    } else {
        this.bubblesort();
        ilist.bubblesort();

        ItemNode a = this.first;
        ItemNode b = ilist.first;

        for(a=first;a!=null;a=a.next) {
            for(b=first;b!=null;b=b.next) {
                if(a.item==b.item) {
                    this.deleteNode(a.item);
                }
            }
        }   
    }
}

private void deleteNode(int data) {
    ItemNode prev = null;

    for(ItemNode trace = first; trace != null; trace = trace.next) {
        if(trace.item == data) {
            if (prev == null) {
                first = trace.next;
            } else {
                prev.next = trace.next;
            }
        } 
        else {
            prev = trace;
        }
    }
}

Lets say i have ItemsList : [0,1,2,3,4] and ilist : [0,1] which means 0 ,1 will be deleted from ItemsList but when i display the ItemsList it says that it's empty . I cannot use arrays , arraylists or other java libraries for the specific problem . Thank you for your time .

b.GHILAS
  • 2,273
  • 1
  • 8
  • 16
  • 1
    Does this answer your question? [java - how to delete a node from linkedlist?](https://stackoverflow.com/questions/22902924/java-how-to-delete-a-node-from-linkedlist) – chargingupfor Nov 28 '19 at 14:08
  • No sorry it doesn't. –  Nov 28 '19 at 14:17
  • 1
    Have you tried to [debug](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems?noredirect=1&lq=1) the issue? You can do it with any IDE of your choice. – Curiosa Globunznik Nov 28 '19 at 14:22

1 Answers1

0

What I would do:

public void remove(ItemsList iList) {
    if (iList.isEmpty()) {
        System.out.println("The list is empty.");
    } else {
        ItemNode prev = null;
        ItemNode a = this.first;
        while (a != null) {
            for (ItemNode b = iList.first; b != null; b = b.next) {
                if (a.item == b.item) prev.next = a.next;
            }
            a = a.next;
        }
    }
}
AP11
  • 617
  • 4
  • 11