使用链接列表实现队列,获取与我的Node类相关的错误

时间:2016-10-31 16:46:07

标签: java linked-list queue

首先是我的节点类,编译很好,我现在用于不同的程序。我已经成功完成了QueueArray,但没有QueueLinked List。

当我尝试编译我的Queue LL时,我不断地得到类Node中的构造函数Node不能应用于给定类型的错误; 节点newNode =新节点(a);

然而,无论我放在那里,我都会遇到错误,并且不知道下一步是什么让我的排队工作。有什么提示吗?



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




3 个答案:

答案 0 :(得分:1)

您正在调用一个不存在的构造函数! Node类中唯一的构造函数是

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

您应该将行Node newNode = new Node(a);更改为Node newNode = new Node(a.getName(), a.getSSN());

答案 1 :(得分:0)

QueueLL类有这一行:

 Node newNode = new Node(a);

它调用Node(Node a)构造函数,但Node类中没有这样的构造函数。

您可以将通话更改为:

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

或在Node类中添加新的构造函数:

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

答案 2 :(得分:0)

使用此行

Node newNode = new Node(a);

您打算通过调用一个构造函数来实例化Node class,该构造函数需要一个已经存在的Node对象。由于没有这样的构造函数,您会收到错误。这是你唯一的构造函数:

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

预计Stringnameintssn。因此,您至少有三种可能的解决方案:

  1. 您创建了一个Node构造函数,用另一个构造Node

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

  2. 您改为调用已存在的构造函数:

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

  3. 您创建了static工厂方法:

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

  4. 并使用它:

    Node newNode = Node.createBy(a);