我是java初学者,目前正在使用Nodes。我想知道是否有一种方法可以显示列表的内容而不必使用 .getNext 方法,因为一旦我使用它,它就会删除节点上的元素,并且实际上会删除顶部的节点。我在这段代码中尝试做的是使用输入将两个字符串元素存储在新的两个节点中,然后使用方法proveTitle来证明这些元素在列表中。一旦我这样做了,我会确保元素仍然完好无损并使用 toString 方法检查列表。请注意,由于某些奇怪的原因,在 Book 类中,如果我将 <> 放在 T 周围,则除了类和实现的类之外,T 不会显示。
继承人的代码:
我的节点类:
public class myNode<T>
{
private T data;
private myNode next;
public myNode(T _data)
{
data = _data;
}
public myNode(T _data, myNode _next)
{
data = _data;
next = _next;
}
public T getData()
{
return data;
}
public void setData(T _data)
{
data = _data;
}
public myNode getNext()
{
return next;
}
public void setNext(myNode _next)
{
next = _next;
}
}
接口类:
public interface myInterface<T>
{
public void pushTitle(T data);
public T pop();
public T peek();
public String toString();
public boolean isEmpty();
public int size();
public myNode getNode();
}
Book 类,其中包含方法
公共类 Book 实现 myInterface
{
private int count;
private T author;
private T title;
private int stock;
private myNode<T> top;
public Book()
{
count = 0;
top = null;
}
@Override
public myNode getNode()
{
return top;
}
@Override
public void pushTitle(T title)
{
myNode<T> current = new myNode<>(title, top);
current.setNext(top);
top = current;
count++;
}
public void proveTitle(T title)
{
T result;
myNode<T> current = top;
if(title.equals(current.getData()))
{
result = current.getData();
System.out.println("The title " + "'" + result + "'" + " exist.");
top = top.getNext();
}
}
@Override
public T pop()
{
T result;
if(count == 0 || top == null )
{
System.out.println("List is empty");
}
System.out.println("The element on top is:" + top.getData());
result = top.getData();
top = top.getNext();
count--;
return result;
}
@Override
public T peek()
{
System.out.println("Element on top is: " + top.getData());
return top.getData();
}
@Override
public boolean isEmpty()
{
if(top == null)
{
System.out.println("The list is empty");
}
else
{
System.out.println("The list is not empty." + "It has" + count + "elements");
}
return top == null;
}
@Override
public int size()
{
System.out.println("The size of the list is" + count);
return count;
}
@Override
public String toString()
{
String result = "";
myNode current = top;
System.out.println("Top");
while(current != null)
{
result += ("[" + current.getData() + "]\n");
current = current.getNext();
}
return result + "Bottom";
}
}
主类:
package node;
import java.util.Scanner;
public class myDriver
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
Book<String> title = new Book<>();
myNode<String> current;
current = title.getNode();
String push;
String push2;
System.out.println("Enter title of book 1");
push = input.nextLine();
title.pushTitle(push);
System.out.println("Enter title of book 2");
push2 = input.nextLine();
title.pushTitle(push2);
title.proveTitle(push);
title.proveTitle(push2);
System.out.println(title.toString());
}
}
输出:
run:
Enter title of book 1
Tiger
Enter title of book 2
crossed
The title 'crossed' exist.
Top
[Tiger]
Bottom
BUILD SUCCESSFUL (total time: 7 seconds)