如何从链表中删除节点

时间:2011-10-07 18:41:35

标签: java eclipse linked-list nodes

嘿伙计们我写了这个deleteNode()方法,如果我使用数字(int)但是当我尝试传入一个字符串时没有。我打印出一个String []名单列表而我是试图从列表中删除某个名称。当我输入名称时,它会打印“未找到节点”。就像我说的,如果我打印出一个数字列表,它的效果很好但是如果我改变并打印一个字符串就不行了。任何帮助表示赞赏。

   public class BigNode {


   public String dataitems; 
    public BigNode next; 
    BigNode front ;

    public void initList(){
        front = null;
    }

    public BigNode makeNode(String number){
        BigNode newNode;
        newNode = new BigNode();
        newNode.dataitems = number;
        newNode.next = null;
        return newNode;
    }

    public boolean isListEmpty(BigNode front){
        boolean balance;
        if (front == null){
            balance = true;
        }
        else {
            balance = false;
        }
        return balance;

    }

    public BigNode findTail(BigNode front) {
        BigNode current;
        current = front;
        while(current.next != null){
            //System.out.print(current.dataitems);
            current = current.next;

        } //System.out.println(current.dataitems);
        return current;
    }

    public void addNode(BigNode front ,String number){
        BigNode tail;
        if(isListEmpty(front)){
            this.front = makeNode(number);
        } 
        else {
            tail = findTail(front);
            tail.next = makeNode(number);
        }
    }
    public void deleteNode(BigNode front, String value) {
        BigNode curr, previous = null; boolean found; 

            if (!isListEmpty(front)){
                curr = front;
                found = false;

                while ((curr.next != null) && (!found)) {
                    if(curr.dataitems.equals(value)) {
                        found = true;
                    } 
                    else {
                        previous = curr;
                        curr = curr.next;
                    }
                }
                if (!found) {
                    if(curr.dataitems.equals(value)) {
                        found = true;
                    }
                }
                if (found) {
                    if (curr.dataitems.equals(front.dataitems)){ // front.dataitems may be wrong .dataitems 
                        front = curr.next;
                    } else { 

                        previous.next = curr.next;
                    }
                } else {
                    System.out.println("Node not found!");
                    //curr.next = null; // Not sure If this is needed
                }
        } 
            showList(front);
    }




    public void printNodes(String[] len){


        int j;
        for (j = 0; j < len.length; j++){

            addNode(front, len[j]);
        }  showList(front);
    }

    public void showList(BigNode front){
        BigNode current;
        current = front;
        while ( current.next != null){
            System.out.print(current.dataitems + ", ");
            current = current.next;
        }
        System.out.println(current.dataitems);
    }
    public static void main(String[] args) {
                   String[] names = {"Billy Joe", "Sally Mae", "Joe Blow", "Tasha Blue"}; 

        BigNode x = new BigNode(); 
                   x.printNodes(names); 
                   Scanner in = new Scanner(System.in);
                   String delete = in.next();
                  x.deleteNode(x.front, delete); 
          }

String [] names = {name1,name2,name3,name4}

- 首先打印列表,然后询问要删除的名称。

4 个答案:

答案 0 :(得分:3)

编辑:好的,我发现你发布的示例代码有什么问题。

您正在调用Scanner.next(),其中只读取一个。您的所有节点值都是两个单词。因此,如果我输入“Sally Mae”,那么实际只是在寻找“Sally”。

这与BigNode中的大部分代码无关(虽然这当然可以更优雅)。基本上这个:

String delete = in.next();

应该是

String delete = in.nextLine();

现在我强烈建议您只是更改代码,而是考虑一下您可以自己诊断出来的方法:

  • 在代码中添加日志记录以显示您要查找的值,以及测试时的每个值
  • 使用调试器逐步执行代码,观察变量
  • 使用单元测试来测试代码 - 那些不会向您显示问题(因为它不是您通常编写测试的代码)但是它们会让您更有信心在测试代​​码中不是

如果您尝试某些或优先所有这些方法,您将从这个问题中学到更多知识,而不仅仅是如何使用Scanner ......


在各个地方,您使用==运算符比较字符串引用。如果您传入对列表中存在的实际字符串对象之一的引用,则只会找到您的节点 - 而不是对相等字符串对象的引用。

你想要这样的东西:

if (curr.dataitems.equals(value))

(但仔细null检查)。

答案 1 :(得分:2)

您应该使用String.equals比较而不是== comparison,。

if(curr.dataitems == value) {

应该是:

if(curr.dataitems.equals(value) {

答案 2 :(得分:1)

您与==进行比较。对于int,这会比较值,但String仅对引用进行比较。所以你想删除一个具有相同值但在不同对象中的String会失败。

改为使用String.equals()

答案 3 :(得分:0)

您应始终使用equals()来比较对象值,而不是==。例如,在你的代码中这一行:

curr.dataitems == value

应该写成:

curr.dataitems.equals(value)