我目前拥有带有三个节点的LinkedStack,其中包含值。这些值是使用我在下面提到的类中创建的push方法创建的。我问的是我推后:“1,2,3”;我有一个3个元素的列表,在我的任务中需要按照从顶部节点到底部的顺序使用条件(当前!= null)等进行更改。但我的问题是如何设置更改元素值而不用使用Arraylist库或用于在主类上使用setMethods缓解那些命令的方法,例如这就是我所做的但显然它不会工作,因为它只是指自己不改变任何东西。我所做的是“代码应该改变你已经在列表中推送的值的值”,行。有人可以解释我在那条线上做错了什么吗?我尝试使用主类上的列表对象来调用LinearNode类方法,但它不会让我,因为它期望给定的变量current等于对象。 祝福你。
public class LinearNode<T>
{
private LinearNode<T> next; //se guarda la direccion del Nodo
private T element; //Lista vacia
public LinearNode()
{
next = null;
element = null;
}
//-----------------------------------------------------------------
// Creates a node storing the specified element.
//-----------------------------------------------------------------
public LinearNode (T elem, LinearNode reference)
{
next = reference;
element = elem;
}
//-----------------------------------------------------------------
// Returns the node that follows this one.
//-----------------------------------------------------------------
public LinearNode<T> getNext()
{
return next;
}
//-----------------------------------------------------------------
// Sets the node that follows this one.
//-----------------------------------------------------------------
public void setNext (LinearNode<T> node)
{
next = node;
}
//-----------------------------------------------------------------
// Returns the element stored in this node.
//-----------------------------------------------------------------
public T getElement()//asigna valor
{
return element;
}
public void setElement(T elem)
{
element = elem;
}
}
public class LinkedStack<T> implements Stack<T> {
private int count;
private LinearNode<T> top; //referencia del Nodo ( direccion)
//-----------------------------------------------------------------
// Creates an empty stack using the default capacity.
//-----------------------------------------------------------------
public LinkedStack()
{
count = 0;
top = null;
}
//-----------------------------------------------------------------
// Removes the element at the top of this stack and returns a
// reference to it. Throws an EmptyCollectionException if the
// stack contains no elements.
//-----------------------------------------------------------------
public LinearNode getLinearNode(){
return top;
}
@Override
public boolean IsEmpty()
{
if(top == null)
{
System.out.println("Stack is empty");
}
return top == null;
}
@Override
public void Push(T element)
{
LinearNode<T> current = new LinearNode<>(element, top);
current.setNext(top);top a la variable de referencia next
top = current;
count++;
}
@Override
public T Pop()
{
T result;
System.out.println("Lets pop the top element!");
if(count == 0)
{
System.out.println("Stack is empty");
}
result = top.getElement();
top = top.getNext();
count--;
System.out.println("The element that we have poped is: " + "'" + result + "'" + "\n");
return result;
}
@Override
public String toString()
{
String result = "";
LinearNode current = top;
System.out.print("<top of stack-->" + "\n");
while (current != null)
{
result += "[" + current.getElement() + "]" + "\n";
current = current.getNext();
}
return result + "<--bottom of stack>" + "\n";
}
@Override
public T Peek() {
System.out.println("Lets peek the top element!");
if(count == 0)
{
System.out.println("Peek failed stack is empty");
}
System.out.println("The element that we have peeked is: " + "[" + top.getElement()+ "]" +"\n");
return top.getElement();
}
@Override
public int Size() {
if(count != 0)
{
System.out.println("Let's check the size of the list!");
System.out.println("The size of the list is: "+ "'" + count + "'" + "\n");
}
if(count == 0)
{
System.out.println("The size of the list is...Woah.");
System.out.println("The list size is now: " + "'" + count + "'" + "\n" + "Push more elements!");
}
return count;
}
}
public class LSmain {
public static void main(String[]args)
{
LinkedStack<Integer> list = new LinkedStack<>();
System.out.println("Let's make a List!");
System.out.println("Push 3 times.");
System.out.println("Check the size.");
System.out.println("Peek the top element.");
System.out.println("Pop three times.");
System.out.println("The size now should be zero!" + "\n");
list.Push(1);
list.Push(2);
list.Push(3);
list.Size();
System.out.println(list.toString());
System.out.println("Change values");
#//#
LinearNode<Integer>current;
current = list.getLinearNode();
while(current != null)
{
current.setNext(current);
current.setElement(5);
}
#//#
System.out.println(list.toString());
答案 0 :(得分:0)
代码注释说getLinearNode
删除堆栈顶部的元素并返回对它的引用,但实际上并没有从堆栈中删除顶层堆栈元素。我假设您的实施是正确的,您的评论不正确或我误解了您的评论。否则,LinkedStack
将需要一个方法,该方法返回对堆栈顶部的引用而不修改堆栈,以便您能够遍历堆栈节点。
LinkedStack stack;
LinearNode current = stack.getLinearNode();
while(current != null) {
current.setElement(T); // I don't know what T is supposed to be here
current = current.getNext();
}