2016-10-01 83 views
-2

我正在尝试创建一个节点队列。每个节点将有2个值(m和n)。 Java相对较新,想知道如何创建/实现一个节点队列,其中每个节点都有一组2个int值(m,n)。在Java中为LinkedList创建节点

+1

您应该封装在一个自己的类的两个值。适当地命名它,并且适当地命名成员属性,以便你的代码的读者知道会发生什么。然后在你的LinkedList中使用这个类(它拥有这些属性)。 – theomega

+0

['队列 myQueue = new ArrayDeque ()'](https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html) – Andreas

+0

Thanks !.用户将输入2个int值 - m和n。这两个值需要作为队列中的单个节点添加。你能详细说明如何编码吗? @theomega –

回答

0

我的方法是如下:

Node { 
    DataType m; 
    DataType n; 
    Node next; // you use to connect to other nodes in the list 
    //constructor{ } 
} 
0

简单的节点列表:

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; 
    } 
}