简体   繁体   English

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

[英]Inserting Node into middle of Linked List, and accidentally inserting null node as well

I'm working on a program that does not use Java's built in Linked List class; 我正在研究一个不使用Java内置的Linked List类的程序; I'm building it from scratch. 我正在从头开始构建它。 I've been successful with everything except writing a method that inserts a Node into a particular position of the linked list. 除了编写一个将Node插入到链表的特定位置的方法之外,我已经成功完成了所有工作。

I have a method that sets a particular Node as the "current" Node. 我有一个方法将特定节点设置为“当前”节点。 So, for example, I have a linked list that looks like this: cats --> dogs --> make --> good --> pets , and "current" is equal to 2; 所以,例如,我有一个链接列表,如下所示: cats - > dogs - > make - > good - > pets ,“current”等于2; that means that the "current" Node is "dogs". 这意味着“当前”节点是“狗”。

From here, let's say I want to insert a new Node at the position of "current" whose info field reads and . 从这里开始,假设我想在“当前”的位置插入一个新节点,其信息字段为 If done correctly, the final linked list will be: cats --> and --> dogs --> make --> good --> pets ; 如果操作正确,最终的链表将是: cats - > - > dogs - > make - > good - > pets ; "and" will replace "dogs" at position 2. “和”将取代第2位的“狗”。

So here's my problem: my method works to insert a new Node at position two, but something's going wrong with linking the newly created node to pre-existing nodes. 所以这是我的问题:我的方法是在第二个位置插入一个新节点,但是将新创建的节点链接到预先存在的节点会出错。 Not only am I inserting my new node into the list, but I'm also inserting a node with no information before "dogs". 我不仅将新节点插入列表中,而且还在“狗”之前插入没有信息的节点。 As my code currently runs, the output looks like this: cats --> and --> (blank) --> dogs --> make --> good --> pets . 正如我的代码目前运行,输出看起来像这样: - > - >(空白) - > - > 制作 - > - > 宠物

I'm 99.9% sure the problem lies in the (if current != null) portion of the code, I just can't figure out how to fix it. 我99.9%肯定问题出在代码的(如果当前!= null)部分,我只是无法弄清楚如何解决它。

Any thoughts on why I'm inserting a blank node in addition to the node I actually want to add? 除了我想要添加的节点之外,还有任何关于我为什么要插入空节点的想法?

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

EDIT 编辑

The entire program is quite long, but here is the "setLine" method which sets current equal to whatever position the user wishes to insert their Node at. 整个程序很长,但这里是“setLine”方法,它将当前设置为用户希望插入其节点的位置。 It takes a parameter "int line" which is obtained via a user prompt. 它采用通过用户提示获得的参数“int line”。

public Node setLine(int line) {

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

Here is a code that inserts the node properly. 这是一个正确插入节点的代码。 This should be a good starting point, good luck(you can read more here: http://www.algolist.net/Data_structures/Singly-linked_list/Insertion ). 这应该是一个很好的起点,祝你好运(你可以在这里阅读更多内容: 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;    
                  }    
            }    
      }    
}

You can refer the following method which inserts node in the middle, based on the index. 您可以参考以下方法,该方法根据索引在中间插入节点。

public boolean insertInMiddle(int index, int data){

    boolean isInserted = false;

    Node node = new Node(data);
    Node temp = head;
    int i=0;
    if(index >= 0 && index <= size()){
        isInserted = true;
        if(index == 0){
            if(head !=null){
                node.nextNode = head;
                head.prevNode = node;
                head = node;
            }else{
                head = node;
                tail=node;
            }
        }else{
            while(i<index){
                temp = temp.nextNode;
                i++;
            }               
            if(temp == null){
                node.nextNode = temp;
                node.prevNode = tail;
                node.prevNode.nextNode = node;
                tail=node;
            }else{
                node.nextNode = temp;
                node.prevNode = temp.prevNode;
                temp.prevNode = node;
                node.prevNode.nextNode = node;
            }
        }
    }       
    return isInserted;
}

//Method to get the size
public int size(){
    int size = 0;

    Node node = head;
    if(node !=null){
        while (node !=null){
            size++;
            node = node.nextNode;
        }
    }

    return size;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM