我是java的初学者,我目前正在使用Nodes。我想知道是否有一种方法来显示列表的内容而不必使用.getNext方法,因为一旦我使用它,它就会删除Node上的元素,从根本上删除顶部的Node。我在这段代码中尝试做的是使用输入在新的两个节点中存储两个String元素,然后使用方法 proveTitle 来证明这些元素在列表中。一旦我这样做,我确保元素仍然完好无损,并使用toString方法检查列表。 请注意,在Book类中有一些奇怪的原因 如果我把<>放在了类和实现的类之外,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();
}
public class 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)
答案 0 :(得分:0)
您似乎尝试使用List
来实施Stack
。这些是非常不同的数据结构。在List
中,您要做的事情非常简单。在Stack
中,它要求您有第二个堆栈。当您从当前堆栈弹出每个对象时,将其推送到第二个堆栈。
如果订单无关紧要,您可以执行此操作一次并切换到使用第二个堆栈。如果订单很重要,您将必须撤消该流程以恢复原始堆栈。
另请注意,更好的方法可能是使用List
。