如何在节点索引之间插入新节点

时间:2017-12-02 21:18:17

标签: java

我是第一次在Java中做面向对象的编程项目 下面是我的一些工作 Quetion:我无法在特定索引中添加新节点。在我的链表类中,我从头开始循环打印节点列表,它显示如下结果:<红色,蓝色,橙色,绿色>但我无法在它们之间放置新节点。例如,我可以在蓝色节点之前添加粉红色的新节点 任何帮助或建议?

ListApp

public class ListApp {

    public static void main(String[] args){

        Node n4 = new Node("green", null);
        Node n3 = new Node("orange", n4);
        Node n2 = new Node("blue", n3);
        Node n1 = new Node("red", n2);

        LinkedList list = new LinkedList(n1);
        System.out.println(list.getHead().getItem());

        System.out.println("\n" );

        list.deleteBefore(n2);
        System.out.println(list);       

    }
}

节点类

package lib;

public class Node {
    private String item;
    private Node nextItem;

    public Node(String str, Node n){
        item = str;
        nextItem = n;
    }
    public String getItem(){
        return item;
    }
    public void setItem(String str){
        item = str;
    }
    public Node next(){
        return nextItem;
    }
    public void setNext(Node n){
        nextItem = n;
    }
}

LinkList类

public class LinkedList {
    private Node head;

    public LinkedList(Node h){
        head = h;
    }
    public Node getHead(){
        return head;
    }
    public void setHead(Node n){
        head = n;
    }

    public void insertAfter(Node newNode, Node prev){
        newNode.setNext(prev.next());
        prev.setNext(newNode);
    }

    public void deleteAfter(Node prev){
        if ( prev.next() != null ){
        prev.setNext(prev.next().next());
        }   
        else prev.setNext(prev.next()); //BECAUSE THERE IS NOTHING TO PRINT OUT.
    }

    public void changeNextItem(Node n, String str){
        if (n.next() != null)
        n.next().setItem(str);
    }

    public void replaceAfter( Node newNode, Node Prev){
        newNode.setNext(Prev.next());
        Prev.setNext(newNode);
    }

    public void changeAll(String colour){
        Node current = head;
        while (current != null){
            current.setItem("white");
            current = current.next();
        }
    }

    public String toString(){
        String str = "";
        Node current = head;
        while(current != null){
            str = str + current.getItem();
            current = current.next();
            if (current != null){
                str = str + ", ";
            }
        }
        return str;
    }
}

1 个答案:

答案 0 :(得分:0)

据我所知,听起来您正试图在某个现有节点之后将新节点插入到LinkedList的中间。您的LinkedList类有一个看起来正确的insertAfter方法。尝试从main()调用该方法。例如,如果您想在节点n3之后添加一个包含字符串“pink”的新节点,您可以这样写:

list.insertAfter(new Node("pink", null), n3);

如果要在给定的现有节点之前添加节点,可以在LinkedList中编写一个非常类似于insertAfter的方法,除非你必须在Node类中添加对前一节点的引用,或者循环通过在insertBefore()方法中找到前一个Node。像这样:

public void insertBefore(Node newNode, Node existingNode){
    Node current = head;
    Node prev = null;
    while(current != null){
        if(current.getItem().equals(existingNode.getItem())){
            prev.setNext(newNode);
            newNode.setNext(current);
            break;
        } else{
            prev = current;
            current = current.next();
        }
    }
}