Firstly, the program used a doubly linked-list which contains ID and NAME lists. Both ID and NAME are parallel. For example, Tony 2456, David 3749, Jessica 9743 and so on.

So I created a method; its function is to remove a specific ID by user input, then it should remove the node which includes both ID and name. Here is my code which seems incorrect. Suggestions?

public static void removeTarget(int IDtarget){
    Node IDfirst = head.IDlink;
    Node IDsecond = head;
    Node NAMEfirst = head.NAMElink;
    Node NAMEsecond = head;
    String NAME;

    if(IDfirst == null || target != IDfirst.id) {
        return;
    }
    else {
        NAME = NAMEfirst.name;
        IDsecond = IDfirst;
        remove(IDsecond.id);
    }

    if(NAME != null) {
        NAMEfirst = NAMEsecond;
        remove(NAME);
    }
}

Dani AI

Generated

Short summary for : the root causes are mixing up an element's value with its position, and trying to assign an integer where a node reference is required. Two safe options: (A) stop using two parallel lists and store id+name in a single node (recommended), or (B) if you must keep parallel lists, compute the index of the name node and remove the ID node at that index. The examples below use new variable names and avoid the patterns in your original posts.

Single-list (recommended) — one Node contains both id and name; removing by name removes both fields together:

class Node {
    int id;
    String name;
    Node next;
    Node(int id, String name){ this.id = id; this.name = name; }
}

static Node removeByName(Node head, String target) {
    Node prev = null, cur = head;
    while (cur != null) {
        if (cur.name.equalsIgnoreCase(target)) {
            if (prev == null) head = cur.next;
            else prev.next = cur.next;
            cur.next = null; // help GC
            return head;
        }
        prev = cur;
        cur = cur.next;
    }
    return head; // not found
}

Parallel-lists (if you cannot change structure) — find the position in the name list, then remove the ID node at that position. Important: store the index (0-based), not the id value.

static int indexOf(NodeName headName, String target) {
    int idx = 0;
    for (NodeName n = headName; n != null; n = n.next, idx++)
        if (n.name.equalsIgnoreCase(target)) return idx;
    return -1;
}

static NodeId removeAt(NodeId headId, int index) {
    if (index < 0 || headId == null) return headId;
    if (index == 0) return headId.next;
    NodeId prev = headId;
    for (int i = 0; i < index - 1 && prev != null; i++) prev = prev.next;
    if (prev == null || prev.next == null) return headId;
    prev.next = prev.next.next;
    return headId;
}

Notes and troubleshooting: always null-check heads before dereferencing, keep a size counter to detect mismatches, be careful with off-by-one when you choose 0-based vs 1-based indexing, and prefer the single-node approach to avoid subtle sync bugs. If you want a recursive version, removeByName can be written to return the new head from a recursive call, but iterative code is simpler and avoids stack-depth issues.

Ignore the post above. Here's my new problem:

Let's say there are two links that contain name and ID and they are both in parallel. For example, Tony 3461, Jessica 8010, Miranda 4914 and so on.

When one name is removed by user input, its ID must be removed as well. How do I store the position value of the element in the "name" list? So I can remove the ID from the ID link using this value. Here is my method:

if(!(target.equalsIgnoreCase(NAME.head.link.name))) {
            return;
        }
        else {
            if(NAME.head != null) {
                storedint = ID.head.link.id; // store the value of the position
                NAME.head = NAME.head.link; // remove the name
            }
        }

        if(ID.head != null) {
            ID.head.link = storedint; // this line is not working. What I need is to remove the ID in the same position as the NAME.
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.