2016-10-26 10 views
0

xを取得し続け、次にノード内にプライベートアクセスがあります。私は周りに遊んでみて、私のNodeクラスと私のLinked Listクラスの両方で何を切り替えても同じエラーが発生し続けます。私は私のノードが別のファイルに保存されており、それはそうのようになります。 ``スタックされたリンク付きリストを使用:ノードエラーでプライベートアクセス

public class Node{ 
 
\t private Node next; 
 
\t private String name; 
 
\t private int ssn; 
 
\t private int key; 
 

 
\t public Node(String name, int ssn){ 
 
\t \t this.name = name; 
 
\t \t this.ssn = ssn; 
 
\t } 
 

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

 
\t public int getSSN(){ 
 
\t \t return this.ssn; 
 
\t } 
 
\t 
 
\t public int getKey(){ 
 
\t \t return ssn%10000; 
 
\t } 
 
\t public String getName(){ 
 
\t \t return name; 
 
\t } 
 
\t 
 
\t public Node getNext(){ 
 
\t \t return this.next; 
 
\t } 
 

 
\t public void setSSN(int ssn){ 
 
\t \t this.ssn= ssn; 
 
\t }

、その後、私はこのようになります私のリンクリストスタックコードがあります。

public class StackLL{ 
 
\t private Node head; //Whatever is on top of the stack 
 
\t private int n; //Suze of the stack 
 
\t private Node next; 
 
\t 
 
\t public StackLL(){ 
 
\t \t head = null; 
 
\t \t n = 0; 
 
\t } 
 
\t 
 

 
\t public boolean isEmpty(){ 
 
\t \t return n == 0; 
 
\t } 
 
\t \t 
 
\t public void push(){ 
 
\t \t Node temp = head; 
 
\t \t head = new Node(); 
 
\t \t head.x = x; 
 
\t \t Node head.next = temp; 
 
\t \t n++; 
 
\t } 
 
\t 
 
\t public Node pop(){ 
 
\t \t Node x = head.x; 
 
\t \t head = head.next; 
 
\t \t n--; 
 
\t \t return x; 
 
\t } 
 
\t \t 
 
\t public Node top(){ 
 
\t \t return head.x; 
 
\t } 
 
\t 
 
\t // printStack method for StackLL 
 
\t public void printStack() { 
 
\t \t System.out.println(n); 
 
\t \t Node temp = head; 
 
\t \t while (temp != null) { 
 
\t \t System.out.println(temp.getKey()); 
 
\t \t temp = temp.getNext(); 
 
     } 
 
    } 
 
\t 
 
}

答えて

0

あなたのコードには、私が見た問題。そう、私はxを発見したpop()内のローカル変数を除き

public class StackLL{ 
    private Node head; //Whatever is on top of the stack 
    private int n; //Suze of the stack 
    // the field next is not used; just delete it 
// private Node next; 

    public StackLL(){ 
     head = null; 
     n = 0; 
    } 

    public boolean isEmpty(){ 
     return n == 0; 
    } 

    public void push(){ 
     Node temp = head; 
     // The Node constructor takes two arguments 
     head = new Node("Jim", 541365250); 
     // x is not defined; delete this statement 
//  head.x = x; 
     // Node.next is not visible (it’s private), so use the setter 
     head.setNext(temp); 
     n++; 
    } 

    public Node pop(){ 
     // if you want to return a Node, just return head 
     Node x = head; 
     // next is private, use the getter 
     head = head.getNext(); 
     n--; 
     return x; 
    } 

    public Node top(){ 
     // if you want to return a Node, just return head 
     return head; 
    } 

    // printStack method for StackLL 
    public void printStack() { 
     System.out.println(n); 
     Node temp = head; 
     while (temp != null) { 
     System.out.println(temp.getKey()); 
     temp = temp.getNext(); 
     } 
    } 

} 

StackLLクラスの数がありました

public void setNext(Node n){ 
    // assign the parameter to the field 
    this.next = n; 
} 

NodeクラスでsetNext()で唯一の単一のもの、ありました私はそれへの参照を削除または変更しました。まだ他のノードにリンクされているノードインスタンスを返すことは、実動コードでは良いアイデアではありませんが、私はそれを今のままにしました。

関連する問題