将Node插入Linked List的中间,并意外插入null节点

Jay*_*ner 9 java singly-linked-list

我正在研究一个不使用Java内置的Linked List类的程序; 我正在从头开始构建它.除了编写一个将Node插入到链表的特定位置的方法之外,我已经成功完成了所有工作.

我有一个方法将特定节点设置为"当前"节点.所以,例如,我有一个链接列表,如下所示:cats - > dogs - > make - > good - > pets,"current"等于2; 这意味着"当前"节点是"狗".

从这里开始,假设我想在"当前"的位置插入一个新节点,其信息字段为.如果操作正确,最终的链表将是:cats - > - > dogs - > make - > good - > pets ; "和"将取代第2位的"狗".

所以这是我的问题:我的方法是在第二个位置插入一个新节点,但是将新创建的节点链接到预先存在的节点会出错.我不仅将新节点插入列表中,而且还在"狗"之前插入没有信息的节点.正如我的代码目前运行,输出看起来像这样: - > - >(空白) - > - > 制作 - > - > 宠物.

我99.9%肯定问题出在代码的(如果当前!= 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();
    }   
}
Run Code Online (Sandbox Code Playgroud)

编辑

整个程序很长,但这里是"setLine"方法,它将当前设置为用户希望插入其节点的位置.它采用通过用户提示获得的参数"int line".

public Node setLine(int line) {

    int index = 0;
    current = first;
    while (index < line) {
        previous = current;
        current = current.next;
        index++;
    }
    return current;
}
Run Code Online (Sandbox Code Playgroud)

Zzz*_*Zzz 1

这是正确插入节点的代码。这应该是一个很好的起点,祝你好运(您可以在这里阅读更多内容: http: //www.algolist.net/Data_structs/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;    
                  }    
            }    
      }    
}
Run Code Online (Sandbox Code Playgroud)