gpt4 book ai didi

java - 使用链表实现队列,出现与我的 Node 类相关的错误

转载 作者:行者123 更新时间:2023-12-01 09:18:26 24 4
gpt4 key购买 nike

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

当我尝试编译我的队列 LL 时,我不断收到错误,类 Node 中的构造函数 Node 无法应用于给定类型;节点 newNode = new Node(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();
}
}
}

最佳答案

您正在调用一个不存在的构造函数! 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());

关于java - 使用链表实现队列,出现与我的 Node 类相关的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40346692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com