本文介绍了Java的:从简单的链表中删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表ItemsList,ilist.如果ilist的节点包含与ItemsList的节点相同的值,则必须从ItemsList删除它们,但是每次使用以下代码中的remove函数时,我的列表都是空的:

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;
        }
    }
}

让我说我有ItemsList:[0,1,2,3,4]和ilist:[0,1],这意味着0,1将被从ItemsList中删除,但是当我显示ItemsList时,它说它是空的.对于特定的问题,我无法使用数组,arraylists或其他Java库.谢谢您的时间.

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 .

推荐答案

我会做什么:

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;
        }
    }
}

这篇关于Java的:从简单的链表中删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:25