Jim Derkin
Jim Derkin

Reputation: 69

Implementing a Queue with a Linked List, Getting error relating to my Node class

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

Upvotes: 0

Views: 757

Answers (3)

Lajos Arpad
Lajos Arpad

Reputation: 77118

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

Upvotes: 0

Bruno
Bruno

Reputation: 3089

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

Upvotes: 1

Marcel
Marcel

Reputation: 1234

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

Upvotes: 0

Related Questions