在链接列表中搜索现有成员时返回布尔结果 - java
Returning boolean result when searching a linked list for an existing member - java
我正在为 class 编写程序,让用户进入无限循环并接受三个不同的命令(添加 x、删除 x、存在 x)+ 一个整数值。当用户输入 "exists x" 时,程序应该 return 一个布尔值结果,指示该值是否存在于列表中。
添加 x 和删除 x 的方法工作得很好,但是 在搜索列表时我无法产生 "true/false" 结果。我已经尝试了几种不同的方法,这是我目前使用现有 x 方法所处的位置:
public class LinkedNode {
public int x; // The data value
public LinkedNode next; // Reference to the next LinkedNode
// Default constructor
LinkedNode() {
next = null;
}
// Constructor that initializes the data values
LinkedNode(int x) {
this.x = x;
}
public void display() {
System.out.print(x + " ");
}
}
public class Set {
public LinkedNode firstLink;
Set(){
firstLink = null;
}
public boolean isEmpty() {
return (firstLink == null); //nothing in Set yet
}
public void add(int x) {
LinkedNode newLink = new LinkedNode(x);
newLink.next = firstLink;
firstLink = newLink;
}
public boolean exists(int x) {
LinkedNode theLink = firstLink;
while (theLink.x != x) { // keep searching until match
if (theLink.next == null) // we've hit the end without a match, return false
return false;
else
theLink = theLink.next;
}
return true;
}
public LinkedNode delete(int x) {
LinkedNode currentLink = firstLink;
LinkedNode previousLink = firstLink;
while (currentLink.x != x) { // search while no match is found
if (currentLink.next == null) {
return null; // not found
} else { // moves to next LinkedNode
previousLink = currentLink;
currentLink = currentLink.next;
}
}
if (currentLink == firstLink) { // first link matches search
firstLink = firstLink.next; // delete link
}
else { // any other link is a match except firstLink
previousLink.next = currentLink.next;
}
return currentLink;
}
public String toString() {
String str = "";
LinkedNode cur = firstLink;
while (cur!=null) {
str += cur.x + " ";
cur = cur.next;
}
return str;
}
}
这是我的 Test/driver:
import java.util.*;
public class Test {
public static void main(String[] args) {
Set dataSet = new Set();
Scanner input = new Scanner(System.in);
while (1<2) { // infinite loop on purpose
String command, value;
System.out.print("Enter command: ");
String line = input.nextLine();
String [] userInput = line.split(" ");
command = userInput [0];
value = userInput [1];
if (!command.equalsIgnoreCase("add") && !command.equalsIgnoreCase("del") && !command.equalsIgnoreCase("exists")){
System.out.println("Invalid command.");
System.out.println("Valid commands are: add x, del x & exists x");
System.out.print("Enter command: ");
}
else if (command.equalsIgnoreCase("add")) {
dataSet.add(Integer.parseInt(value));
System.out.println(dataSet);
}
else if (command.equalsIgnoreCase("del")){
dataSet.delete(Integer.parseInt(value));
System.out.println(dataSet);
}
else if (command.equalsIgnoreCase("exists"));{
dataSet.exists(Integer.parseInt(value));
System.out.println(dataSet);
}
}
如果 link 中存在搜索的值,我希望程序显示 "true/false",类似于 add/delete 之后显示当前列表的方式。关于我哪里出错的任何提示?
Enter command: add 3
3
Enter command: add 1
1 3
Enter command: add 20
20 1 3
Enter command: exists 20
Enter command:
您的函数可能返回了正确的布尔值。但是您没有打印结果。您可以使用以下类型的代码。
if(dataSet.exists(Integer.parseInt(value))==True)
{
system.out.println("exists");
}
else
{
system.out.println("not exists");
}
以上代码只是伪代码
你真的很接近,你的代码实际上工作正常,只是你有一个小错字,它被扔掉了!
else if (command.equalsIgnoreCase("exists"));{
此代码末尾包含一个分号,不幸的是这是有效的 Java,因此不会抛出任何编译时异常。尽管您通常会收到一条警告:
Blockquote 'if' statement has empty body
此外,因为这是在执行每个循环,所以您的结果会显示两次,因此删除分号应该可以解决问题:
else if (command.equalsIgnoreCase("exists")) {
System.out.println(dataSet.exists(Integer.parseInt(value)));
System.out.println(dataSet);
}
在代码示例中,为了清楚起见,我打印了该值是否存在于 LinkedList 中的结果。
注意: 每次要求用户输入命令以允许拆分不同的命令时,可能还值得添加一个新行:
System.out.print("\nEnter command: ");
再说一次,这纯粹是装饰性的,只是让您的输出更具可读性!
感谢 Andreas & M H 的帮助 - 以下是生成预期输出的内容:
else if (command.equalsIgnoreCase("exists")){
if(dataSet.exists(Integer.parseInt(value)))
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
首先,你应该打印你想要的值,这将帮助你检查该方法是否有效。
else if(command.equalsIgnoreCase("exists"){
if(dataSet.exists(Integer.parseInt(value)))
System.out.println("true");
else
System.out.println("false");`enter code here`
如果列表 (Set) 为空,您也应该拒绝,这可能是代码无法正常工作的原因。此外,如果您将局部变量用作私有变量会更正确。更改它们并为它们编写方法 setX(x), getX() 。关于方法存在,我认为更好的代码是这个:
public Boolean exists(int x){
LinkedNode theLink = firstLink;
Boolean resul = false;
while(!isEmpty() && theLink.getNext() != null){
if(link.getX() != x)
theLink = theLink.getNext();
else
resul = true;
}
return resul;
如果您不想将本地 linkedNode 变量设置为私有,只需将 getNext 和 getX 更改为 .next 和 .x
尝试理解代码,下次自己尝试更多时间,是唯一的学习方法。
我正在为 class 编写程序,让用户进入无限循环并接受三个不同的命令(添加 x、删除 x、存在 x)+ 一个整数值。当用户输入 "exists x" 时,程序应该 return 一个布尔值结果,指示该值是否存在于列表中。
添加 x 和删除 x 的方法工作得很好,但是 在搜索列表时我无法产生 "true/false" 结果。我已经尝试了几种不同的方法,这是我目前使用现有 x 方法所处的位置:
public class LinkedNode {
public int x; // The data value
public LinkedNode next; // Reference to the next LinkedNode
// Default constructor
LinkedNode() {
next = null;
}
// Constructor that initializes the data values
LinkedNode(int x) {
this.x = x;
}
public void display() {
System.out.print(x + " ");
}
}
public class Set {
public LinkedNode firstLink;
Set(){
firstLink = null;
}
public boolean isEmpty() {
return (firstLink == null); //nothing in Set yet
}
public void add(int x) {
LinkedNode newLink = new LinkedNode(x);
newLink.next = firstLink;
firstLink = newLink;
}
public boolean exists(int x) {
LinkedNode theLink = firstLink;
while (theLink.x != x) { // keep searching until match
if (theLink.next == null) // we've hit the end without a match, return false
return false;
else
theLink = theLink.next;
}
return true;
}
public LinkedNode delete(int x) {
LinkedNode currentLink = firstLink;
LinkedNode previousLink = firstLink;
while (currentLink.x != x) { // search while no match is found
if (currentLink.next == null) {
return null; // not found
} else { // moves to next LinkedNode
previousLink = currentLink;
currentLink = currentLink.next;
}
}
if (currentLink == firstLink) { // first link matches search
firstLink = firstLink.next; // delete link
}
else { // any other link is a match except firstLink
previousLink.next = currentLink.next;
}
return currentLink;
}
public String toString() {
String str = "";
LinkedNode cur = firstLink;
while (cur!=null) {
str += cur.x + " ";
cur = cur.next;
}
return str;
}
}
这是我的 Test/driver:
import java.util.*;
public class Test {
public static void main(String[] args) {
Set dataSet = new Set();
Scanner input = new Scanner(System.in);
while (1<2) { // infinite loop on purpose
String command, value;
System.out.print("Enter command: ");
String line = input.nextLine();
String [] userInput = line.split(" ");
command = userInput [0];
value = userInput [1];
if (!command.equalsIgnoreCase("add") && !command.equalsIgnoreCase("del") && !command.equalsIgnoreCase("exists")){
System.out.println("Invalid command.");
System.out.println("Valid commands are: add x, del x & exists x");
System.out.print("Enter command: ");
}
else if (command.equalsIgnoreCase("add")) {
dataSet.add(Integer.parseInt(value));
System.out.println(dataSet);
}
else if (command.equalsIgnoreCase("del")){
dataSet.delete(Integer.parseInt(value));
System.out.println(dataSet);
}
else if (command.equalsIgnoreCase("exists"));{
dataSet.exists(Integer.parseInt(value));
System.out.println(dataSet);
}
}
如果 link 中存在搜索的值,我希望程序显示 "true/false",类似于 add/delete 之后显示当前列表的方式。关于我哪里出错的任何提示?
Enter command: add 3
3
Enter command: add 1
1 3
Enter command: add 20
20 1 3
Enter command: exists 20
Enter command:
您的函数可能返回了正确的布尔值。但是您没有打印结果。您可以使用以下类型的代码。
if(dataSet.exists(Integer.parseInt(value))==True)
{
system.out.println("exists");
}
else
{
system.out.println("not exists");
}
以上代码只是伪代码
你真的很接近,你的代码实际上工作正常,只是你有一个小错字,它被扔掉了!
else if (command.equalsIgnoreCase("exists"));{
此代码末尾包含一个分号,不幸的是这是有效的 Java,因此不会抛出任何编译时异常。尽管您通常会收到一条警告:
Blockquote 'if' statement has empty body
此外,因为这是在执行每个循环,所以您的结果会显示两次,因此删除分号应该可以解决问题:
else if (command.equalsIgnoreCase("exists")) {
System.out.println(dataSet.exists(Integer.parseInt(value)));
System.out.println(dataSet);
}
在代码示例中,为了清楚起见,我打印了该值是否存在于 LinkedList 中的结果。
注意: 每次要求用户输入命令以允许拆分不同的命令时,可能还值得添加一个新行:
System.out.print("\nEnter command: ");
再说一次,这纯粹是装饰性的,只是让您的输出更具可读性!
感谢 Andreas & M H 的帮助 - 以下是生成预期输出的内容:
else if (command.equalsIgnoreCase("exists")){
if(dataSet.exists(Integer.parseInt(value)))
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
首先,你应该打印你想要的值,这将帮助你检查该方法是否有效。
else if(command.equalsIgnoreCase("exists"){
if(dataSet.exists(Integer.parseInt(value)))
System.out.println("true");
else
System.out.println("false");`enter code here`
如果列表 (Set) 为空,您也应该拒绝,这可能是代码无法正常工作的原因。此外,如果您将局部变量用作私有变量会更正确。更改它们并为它们编写方法 setX(x), getX() 。关于方法存在,我认为更好的代码是这个:
public Boolean exists(int x){
LinkedNode theLink = firstLink;
Boolean resul = false;
while(!isEmpty() && theLink.getNext() != null){
if(link.getX() != x)
theLink = theLink.getNext();
else
resul = true;
}
return resul;
如果您不想将本地 linkedNode 变量设置为私有,只需将 getNext 和 getX 更改为 .next 和 .x 尝试理解代码,下次自己尝试更多时间,是唯一的学习方法。