【问题标题】:Need guidance on creating Node class (java)?需要有关创建 Node 类 (java) 的指导吗?
【发布时间】:2017-01-30 08:54:16
【问题描述】:

我需要实现一个Node类,其中的基本方法是:getItem()、getNext()、setItem()和setNext()。我希望节点能够至少将 Java 中的默认整数范围存储为“项目”; “next”应该是指向链表中下一个节点的引用或指针,或者如果这是列表中的最后一个节点,则为特殊节点 NIL。我还想实现一个双参数构造函数,它使用给定的实例初始化实例item(第一个参数)和下一个节点(第二个参数),我有点碰壁了,需要一些关于实现这个的指导,有什么想法吗?

到目前为止我有这个:

class Node {


public Node(Object o, Node n) {

}
public static final Node NIL = new Node(Node.NIL, Node.NIL);

public Object getItem() {
    return null;
}
public Node getNext() {
    return null;
}
public void setItem(Object o) {

}
public void setNext(Node n) {

}
}

【问题讨论】:

标签: java nodes


【解决方案1】:

在实现自定义 LinkedList/Tree 时,我们需要 Node.js。这是创建 Node 和 LinkedList 的演示。我没有把所有的逻辑都放进去。这里只有基本骨架,然后您可以自己添加更多内容。

【讨论】:

  • 请不要对代码使用屏幕截图。相反,将代码粘贴到您的答案中,选择它,然后在编辑器中按{} 按钮。
  • @ChaiT.Rex 感谢您提供的信息 :) 我会从下一次开始处理这个问题。
【解决方案2】:

我可以给你一个快速提示:

Class Node{

    //these are private class attributes, you need getter and setter to alter them.
    private int item;
    private Node nextNode;

    //this is a constructor with a parameter
    public Node(int item)
    {
        this.item = item;
        this.nextNode = null;
    }

    // a setter for your item 
    public void setItem(int newItem)
    {
        this.item = newItem;
    }

    // this is a getter for your item
    public int getItem()
    {
        return this.item;
    }   

}

你可以通过调用来创建一个Node对象:

Node newNode = Node(2);

这不是您问题的完整解决方案,缺少两个参数构造函数和最后一个节点链接,但这应该会引导您朝着正确的方向前进。

【讨论】:

    【解决方案3】:

    下面是 Node 实现的一个简单示例(为了便于阅读,我将 Item 重命名为 Value)。它必须以某种方式实现,因为方法签名似乎是强加给你的。但请记住,这绝对不是实现 LinkedList 的最佳方式。

    public class Node {
        public static final Node NIL = null;
        private Integer value;
        private Integer next;
    
        public Node(Integer value, Node next) {
            this.value = value;
            this.next = next;
        }
    
        public Integer getValue() {
            return this.value;
        }
        public Node getNext() {
            return this.next;
        }
    
        public void setValue(Integer value) {
            this.value = value;
        }
    
        public void setNext(Node next) {
            this.next = next;
        }
    
        public boolean isLastNode() {
            return this.next == Node.NIL || Node;
        }
    }
    
    
    public class App {
        public static void main(String[] args) {
            Node lastNode = new Node(92, Node.NIL);
            Node secondNode = new Node(64, lastNode);
            Node firstNode = new Node(42, secondNode);
    
            Node iterator = firstNode;
            do () {
                System.out.println("node value : " + iterator.getValue());
                iterator = iterator.getNext();
            } while (iterator == null || !iterator.isLastNode());
        }
    }
    

    【讨论】:

    • Node.NIL 是一个对象,而不是一个方法调用。因此,它不会导致堆栈溢出。
    • @ChaiT.Rex 是的,它是Node NIL = new Node(Node.NIL, Node.NIL)。仔细看,构造函数将调用自己。要正确构建,Node.NIL 需要 Node.NIL。这将导致堆栈溢出。
    • 我实际上已经运行了代码。它不会导致堆栈溢出。您可能也应该运行代码而不是猜测。
    • 嗯...对,我不知道为什么它会起作用,但是,我会编辑我的答案。谢谢。
    【解决方案4】:

    要实现的节点类会根据你要实现的链表而变化。如果您要实现的链表是循环的,那么您可以执行以下操作:

    public class Node {
        
        int data;
        Node next = null;
        
        public Node(int data){
            this.data = data;
        }
    }
    

    那你打算怎么实现下一个节点呢?

    您将在 circularLinkedList 类的 add 方法中执行此操作。你可以这样做:

    import java.util.*;
    
    public class CircularLinkedList {
        
        public CircularLinkedList() {}
        
        public Node head = null;
        public Node tail = null;
        
        public void add(int data) {
            
            Node newNode = new Node(data);
            
            if(head == null) {
                head = newNode;
    
            }
            else {
            
                tail.next = newNode;
                
            }
            
            tail = newNode;
            tail.next = head;
        }
        
        public void displayList() {
            System.out.println("Nodes of the circular linked list: ");  
            
            Node current = head;
            
            if(head == null) {
                System.out.println("Empty list...");
            }
            
            else {
                
                do {
                    
                    System.out.print(" " + current.data);
                    current = current.next;
                    
                }while(current != head);
                System.out.println();
            }
            
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      • 2012-08-10
      • 2014-10-28
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      相关资源
      最近更新 更多