在Java中为LinkedList创建节点
我正在尝试创建一个节点队列。每个节点将有2个值m和n。对Java比较陌生,想知道如何创建/实现一个节点队列,其中每个节点都有一组2个int值m,n。我的方法如下:在Java中为LinkedList创建节点,java,data-structures,queue,Java,Data Structures,Queue,我正在尝试创建一个节点队列。每个节点将有2个值m和n。对Java比较陌生,想知道如何创建/实现一个节点队列,其中每个节点都有一组2个int值m,n。我的方法如下: Node { DataType m; DataType n; Node next; // you use to connect to other nodes in the list //constructor{ } } 简单节点列表: public class List{ class Node{ p
Node {
DataType m;
DataType n;
Node next; // you use to connect to other nodes in the list
//constructor{ }
}
简单节点列表:
public class List{
class Node{
protected int a, b;
Node next;
public Node(int a, int b){
this.a = a;
this.b = b;
}
//some get methods
}
Node root = null;
public void insertNode(int a, int b){
new_node = new Node(a, b);
new_node.next = root;
root = new_node;
}
}
您应该将这两个值封装在自己的类中。适当地命名它,并适当地命名成员属性,这样代码的读者就知道会发生什么。然后只需使用保存LinkedList中属性的类即可。谢谢!。用户将输入2个int值-m和n。这2个值需要作为队列中的单个节点添加。你能详细说明一下如何编写代码吗@theomega@KeshavSharma您知道如何创建具有字段的类吗?如果是,那么就这样做。如果没有,请进一步阅读Java指南。