java - 将节点插入链表中间,不小心也插入了空节点

标签 java singly-linked-list

我正在开发一个不使用 Java 内置链表类的程序;我正在从头开始构建它。除了编写一个将节点插入到链表的特定位置的方法之外,我在所有方面都取得了成功。

我有一个方法可以将特定节点设置为“当前”节点。因此,例如,我有一个如下所示的链表:cats --> dogs --> ma​​ke --> good --> pets,"current"等于2;这意味着“当前”节点是“狗”。

从这里开始,假设我想在“当前”位置插入一个新节点,其信息字段为。如果正确完成,最终的链表将是:cats --> and --> dogs --> ma​​ke --> --> 宠物; “and”将替换位置 2 的“dogs”。

所以这是我的问题:我的方法可以在第二个位置插入一个新节点,但是将新创建的节点链接到预先存在的节点时出现问题。我不仅将我的新节点插入到列表中,而且还在“dogs”之前插入一个没有任何信息的节点。当我的代码当前运行时,输出如下所示:cats --> and -->(空白)--> dogs --> 制作 --> --> 宠物

我 99.9% 确定问题出在代码的 (if current != null) 部分,我只是不知道如何修复它。

关于为什么除了我实际想要添加的节点之外还要插入一个空白节点,有什么想法吗?

public void insert () {

    System.out.println("Please enter the text you wish to insert, or type \"end\" if you are done inserting.");
    String theString;
    theString = console.nextLine();

    while (!theString.equals("end")){
        newNode = new Node ();
        newNode.info = theString;
        newNode.next = null;

        if (first == null){
            first = newNode;
            last = newNode;
        } else if (current != null){
            Node p = new Node (current.info, current.next);
            current.info = newNode.info;
            current.next = p;
        }
        else {
            last.next = newNode;
            last = newNode;
        }

        System.out.println("Please enter the text you wish to insert, or type \"end\" if you are done inserting.");
        theString = console.nextLine();
    }   
}

编辑

整个程序很长,但这里有一个“setLine”方法,它将电流设置为用户希望插入节点的任何位置。它采用通过用户提示获取的参数“int line”。

public Node setLine(int line) {

    int index = 0;
    current = first;
    while (index < line) {
        previous = current;
        current = current.next;
        index++;
    }
    return current;
}

最佳答案

这是正确插入节点的代码。这应该是一个很好的起点,祝你好运(你可以在这里阅读更多内容:http://www.algolist.net/Data_structures/Singly-linked_list/Insertion)。

public class SinglyLinkedList {

      public void addLast(SinglyLinkedListNode newNode) {    
            if (newNode == null)    
                  return;    
            else {    
                  newNode.next = null;    
                  if (head == null) {    
                        head = newNode;    
                        tail = newNode;    
                  } else {    
                        tail.next = newNode;    
                        tail = newNode;    
                  }    
            }    
      }

      public void addFirst(SinglyLinkedListNode newNode) {    
            if (newNode == null)    
                  return;    
            else {    
                  if (head == null) {    
                        newNode.next = null;    
                        head = newNode;    
                        tail = newNode;    
                  } else {    
                        newNode.next = head;    
                        head = newNode;    
                  }    
            }    
      }

      public void insertAfter(SinglyLinkedListNode previous,    
                  SinglyLinkedListNode newNode) {    
            if (newNode == null)    
                  return;    
            else {    
                  if (previous == null)    
                        addFirst(newNode);    
                  else if (previous == tail)   
                        addLast(newNode);    
                  else {    
                        SinglyLinkedListNode next = previous.next;    
                        previous.next = newNode;    
                        newNode.next = next;    
                  }    
            }    
      }    
}

关于java - 将节点插入链表中间,不小心也插入了空节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13262333/

相关文章:

c - 在我的 c 代码中删除重复项时出现错误

java - 在java中使用单链表和冒泡排序进行编码

java - 如何从 JUnit 内部使用 Voldemort 服务器?

java - 使用 web3j 检查 ERC20 合约中的余额

java - 比较两个 String[] 数组并打印出不同的字符串

java - 学习 LinkedList 和 Nodes,但我在 buildList 方法的 newNode 上不断收到 "cannot be resolved or is not in field"错误

C++单链表复制构造函数段错误

java - 关于缺少 javax/mail/mail/1.4/activation.jar 的 Maven 编译错误。怎么修?

java - 如何获取对象内部的对象?

C:内存泄漏实现一个简单的链表