Jim Jim - 1 month ago 7
Node.js Question

using a Linked List with a Stack: private acces in node error

I keep getting the x and next have a private access in Node. I tried playing around and keep getting the same error no matter what I switch around in both my Node class and my Linked List class. I have my node saved in a different file and it looks like so:
``



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

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

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

public int getSSN(){
return this.ssn;
}

public int getKey(){
return ssn%10000;
}
public String getName(){
return name;
}

public Node getNext(){
return this.next;
}

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





and then I have my Linked List stack code which looks like this:



public class StackLL{
private Node head; //Whatever is on top of the stack
private int n; //Suze of the stack
private Node next;

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


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

public void push(){
Node temp = head;
head = new Node();
head.x = x;
Node head.next = temp;
n++;
}

public Node pop(){
Node x = head.x;
head = head.next;
n--;
return x;
}

public Node top(){
return head.x;
}

// 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();
}
}

}




Answer

I inserted comments in your code about the issues I saw. In the Node class there was only a single one, in setNext():

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

There were a number in the StackLL class:

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

}

Except for the local variable in pop() I found no x, so I removed or changed references to it. Returning Node instances that are still linked to other nodes would not be a good idea in production code, but I have let it be for now.