0

First is my node class, which compiles fine and I've used for different programs now. I have done a QueueArray with success but not a QueueLinked List.

When i try to compile my Queue LL, i constantly keep getting the error that constructor Node in class Node cannot be applied to given types; Node newNode = new Node(a);

However, no matter what I put there, I keep getting errors and just have no idea what the next step is to make my enqueue work. Any tips?

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

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

public class QueueLL{
	private Node first;
	private Node last;
	private int n;
	private Node queue;
	
	public QueueLL(){
		first = null;
		last = null;
		n = 0;
	}
		
	public boolean isEmpty(){
		return first == null;
	}
	
	public Node front(){
		return first;
	}
		
	public void enqueue(Node a){
		Node newNode = new Node(a);
		if (first == null){
			first = a;
			last = first;
		}
		else{
			last = a.getNext();
			last = a;
	}
}

	public Node dequeue(){
		if (first == null){
			return null;
		}
		else{
			Node temp = first;
			first = first.getNext();
			return temp;
		}
	}
	// printQueue method for QueueLL
    public void printQueue() {
        System.out.println(n);
        Node temp = first;
        while (temp != null) {
            System.out.println(temp.getKey());
            temp = temp.getNext();
        }
    }
}

6
  • 3
    There's no constructor that accepts another Node. Why would you expect it to compile? Commented Oct 31, 2016 at 16:50
  • 2
    "I keep getting errors and just have no idea what the next step is to make my enqueue work. Any tips?" -- consider telling and showing all error messages. They're kind of important. Commented Oct 31, 2016 at 16:51
  • @HovercraftFullOfEelse this is the only error I get QueueLL.java:22: error: constructor Node in class Node cannot be applied to given types; Node newNode = new Node(a); ^ required: String,int found: Node reason: actual and formal argument lists differ in length 1 error Commented Oct 31, 2016 at 16:52
  • @shmosel I'm still very new to java. can you explain how I can go about having my constructor accept another node? Commented Oct 31, 2016 at 16:52
  • Jim: that information is much too important to be buried in comments. Consider editing your original post. Regardless, in the future, please include all relevant information in your original post. Makes sense, no? Commented Oct 31, 2016 at 16:52

3 Answers 3

1

You are calling a constructor that doesnt exist! The only constructor you have in Node class is

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

You should change the line Node newNode = new Node(a); to Node newNode = new Node(a.getName(), a.getSSN());

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this helped me compile, yet I seem to not be able to run my code properly when I do the test. My code only prints out one node, and therefore I think my enqueue function isn't 100% yet. Any idea if theres something else off in my enqueue function?
Dont forget to vote the answer! Could paste here your main? I'll try to debug and see where is the error, but I think your not setting node.setNext
0

The QueueLL class has this line:

 Node newNode = new Node(a);

It calls the Node(Node a) constructor, but there is no such constructor in the Node class.

You can change the call to:

Node newNode = new Node(a.getName(), a.getSSH());

or add a new contructor the the Node class:

public Node(Node node){
    this.name = node.getName();
    this.ssn = node.getSSH();
}

1 Comment

I'm able to compile, but when I run a test code to see if my code works out, all i print out is 6789, which is only one node. I think there's an error within my Enqueue function then where new nodes are still not actually being created. Is this correct for enqueue? public void enqueue(Node a){ Node newNode = new Node(a.getName(), a.getSSN()); if (first == null){ first = a; last = first; } else{ last = a.getNext(); last = a; } }
0

With this line

Node newNode = new Node(a);

you intend to instantiate the Node class by calling a constructor which expects an already existent Node object. Since there is no such constructor, you get the error. This is your only constructor:

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

It expects a String for name and an int for ssn. As a result you have at least three possible solutions:

  1. You create a Node constructor which constructs a Node by another one:

    public Node(Node input) { this.setName(input.getName()); this.setSSN(input.getSSN()); this.setNext(input.getNext()); }

  2. You call the already existent constructor instead:

    Node newNode = new Node(a.getName(), a.getSSN());

  3. You create a static factory method:

    public static Node createBy(Node input) { Node output = new Node(input.getName(), input.getSSN()); output.setNext(input.getNext()); return output; }

and use it:

Node newNode = Node.createBy(a);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.