最后添加

时间:2013-09-17 15:52:21

标签: java

感谢您抽出宝贵时间阅读本文,我正在学习Java课程,教授告诉我们理解链接的一个好习惯是制作双重链表。我已经制作了一个单独的链接列表,但我无法将其转换为双向链表。我遇到的问题是我正在尝试在列表的末尾添加一个数字,但是当我尝试在结尾添加一个新数字时,它只显示数字而不是其他数字。我想我只是创建一个新列表并显示它但我不确定。我们非常感谢代码示例。

正常添加代码:

 public void add(int element){

            Node n = new Node();
            n.setItem(element);
            n.setNext(head); 
            n.setBefore(null); 
            if(head != null) {

                head.setBefore(n); 
            }
            head = n;

        }

添加到最后一段代码:

public void addLast(int element) {

        Node currentNode = head;


        currentNode.setItem(element);
        currentNode.setBefore(tail); 
        currentNode.setNext(null);


        tail = currentNode;

    }

完整代码:

public class DoubleLink {

private Node head; 
private Node tail;

    //Methods
    //Constructors
    public DoubleLink(){
        head = null;
        tail = null;

    }

    public void add(int element){

            Node n = new Node();
            n.setItem(element);
            n.setNext(head); 
            n.setBefore(null); 
            if(head != null) {

                head.setBefore(n); 
            head = n;


        }
    public void display(){      //LIST TRAVERSAL!
        // Reference traversal
        //Needed an interatior
        Node currentNode = head;
        while(currentNode != null){
            System.out.print(currentNode.getItem()+ " ");
            currentNode = currentNode.getNext();
        }
        System.out.println();
    }


    public int search(int element){
        int position = 0;
        Node currentNode = head;
        while(currentNode != null){
            if(currentNode.getItem() == element){
                return position;

            }
            position++;
            currentNode = currentNode.getNext();

        }
        return -1;
    }

    public void insert(int element, int position){
        int currentposition = 0;
        Node currentNode = head;

        //Traverse to the right position
        while(currentposition < position-1){

            currentposition++;
        } 
        Node n = new Node();
        n.setItem(element);
        n.setNext(currentNode.getNext());
        currentNode.setNext(n);

        //The previous number connecting to the new number
        currentNode = tail;

    }

    public void remove(int position){
        int currentposition = 0;
        Node currentNode = head;

        //Traverse to the right position
        while(currentposition < position-1){

            currentposition++;
        }

        Node dyingNode = currentNode.getNext();
        currentNode.setNext(dyingNode.getNext());


    }

    public void addLast(int element) {



        Node nodeToInsert = new Node();
        nodeToInsert.setItem(element);
        nodeToInsert.setBefore(tail); 
        nodeToInsert.setNext(null);

        if(tail != null)
          tail.setNext(nodeToInsert); //link the list

        tail = nodeToInsert; //now the tail is the new node i added

        if(head == null)       // if the list has no elements then set the head
             head = nodeToInsert;


    } 

    public static void main(String[] args) {
    DoubleLink l = new DoubleLink();


    l.add(1);
    l.add(2);
    l.add(3);
    l.display();
    l.addLast(99);
    l.display();

    }
}

节点类:

public class Node {

    //Data

    private int item;
    private Node next;
    private Node before;

    //Methods
    public int getItem(){
        return item;
    }

    public void setItem(int item){
        this.item = item;
    }

    public Node getNext(){
        return next;
    }

    public void setNext (Node next){
        this.next = next;
    }

    public Node getBefore(){
        return before;
    }

    public void setBefore(Node before){
        this.before = before;
    }

}

3 个答案:

答案 0 :(得分:3)

您应该更改代码,像这样创建new Node()

public void addLast(int element) {

        Node nodeToInsert = new Node();
        nodeToInsert.setItem(element);
        nodeToInsert.setBefore(tail); 
        nodeToInsert.setNext(null);

        if(tail != null)
          tail.setNext(nodeToInsert); //link the list

        tail = nodeToInsert; //now the tail is the new node i added

        if(head == null)       // if the list has no elements then set the head
             head = nodeToInsert;
}

<强>更新

问题在于您的添加方法,您没有设置永远tail

public void add(int element){

            Node n = new Node();
            n.setItem(element);
            n.setNext(head); 
            n.setBefore(null); 
            if(head != null) 
                head.setBefore(n); 
            else{
                tail=n; // if head == null then now you have an element so head = tail
            }

            head = n;        
    }

答案 1 :(得分:1)

试试这个:

public void addLast(int element) {

    Node newNode = new Node();

    newNode.setItem(element);
    newNode.setNext(null);
    newNode.setBefore(tail);

    tail.setNext(newNode);

    tail = newNode;
}

答案 2 :(得分:1)

在addlast(int元素)中,您没有为刚刚覆盖的新节点创建内存 头节点。 所以,你的代码就像这样

public void addlist(int element)
{
    Node currentNode = new node();


    currentNode.setItem(element);
    currentNode.setBefore(tail); 
    currentNode.setNext(null);
    if(tail!=null)
         tail.setNext(currentNode);


    tail = currentNode;
}