【问题标题】:Java Linked List add MethodJava链表添加方法
【发布时间】:2020-03-27 04:13:25
【问题描述】:

我正在尝试实现一个使用包含头、尾和当前节点的节点类的链表。链表的一部分是一个 add 方法,它应该向链表中当前节点的末尾添加一个值,就像实际链表一样。我的问题是它只适用于第一个节点,然后就停在那里。例如,我主要尝试通过调用add(1);add(2); 来测试代码。控制台向我显示1,仅此而已。我不确定错误是在我的 add 方法、toString 方法还是节点类中。

我还要补充一点,在任何一种情况下,我都测试了是否将正确的值分配给“当前”,并且确实如此。这让我想知道问题的根源是否是 toString,但是无论我如何尝试,我都无法对其进行更改以进行任何改进。

我希望新的眼睛能够发现任何可能存在的明显问题。

添加方法:

public void add(int val){
    if(current != null){
        Node nextNode = new Node(val, current);
        current = nextNode;
        tail = nextNode;                    
    }
    else{
        head = tail = new Node(val, null);
        current = head;
    }
}

节点类:

public class Node{
    public int data;
    public Node next;

    public Node(int d, Node next) {
        this.data = d;
        this.next = next;
    }
}

toString:

public String toString(){
    for(Node x = head; x != null; x = x.next){
        System.out.println(x.data);
}

全部:

public class IntLList extends IntList{    

    public IntLList(){

    }

    public class Node{
        public int data;
        public Node next;

         public Node(int d, Node next) {
            this.data = d;
            this.next = next;
        }
    }

    Node head = null;
    Node tail = null;
    Node current = null;

    public void add(int val){
        if(current != null){
            Node nextNode = new Node(val, current);
            current = nextNode;
            tail = nextNode;    
        }
        else{
            head = tail = new Node(val, null);
            current = head;        
        }
    }

    public int get(int index){
        return 0;
    }

    public void set(int index, int val){
    }

    public void remove(int index) throws ArrayIndexOutOfBoundsException{
    }

    public int size(){
        return 0;
    }

    public String toString(){
        for(Node x = head; x != null; x = x.next){
            System.out.println(x.data);
        }
        return "temp";
    }

    public void removeLast(){
    }

    public boolean isEmpty(){
        boolean isEmpty = false;

        if(head == null){       
            isEmpty = true;
        }

        return isEmpty;
    }

    public void clear(){    
    }

    public static void main(String[] args) {
        IntLList i = new IntLList();
        i.add(1);
        i.add(2);
        i.toString();
    }
}

【问题讨论】:

    标签: java data-structures linked-list


    【解决方案1】:

    进行以下更改:

    public class Node{
    
        public int data;
        public Node next;
    
        public Node(int d, Node next) {
    
            this.data = d;
            this.next = NULL; // this is to set the next node of current node to null
            if(next!=NULL)
                next.next=this; // this is to set the previous node to point to current node
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      • 1970-01-01
      • 2015-07-03
      相关资源
      最近更新 更多