Java 更改节点的值
我目前有三个带有值的节点的LinkedStack。这些值是使用我在下面提到的类中创建的push方法创建的。我的问题是在我按下“1,2,3”键之后;我有一个包含3个元素的列表,在我的赋值中,需要使用while(current!=null)条件从顶部节点到底部按顺序更改这些元素等等。但我的问题是,我如何在不使用Arraylist库或此类方法的情况下更改元素值,这些方法用于在主类上使用setMethods简化此类命令。例如,这就是我所做的,但显然它不起作用,因为它只是指它本身,不改变任何东西。我所做的是通过“代码应该在哪里更改列表中已经推送的值”行。有人能解释一下我在那条线上做错了什么吗?我尝试在main类上使用list对象调用LinearNode类方法,但它不允许,因为它期望给定的变量current等于该对象。 祝福你 线性节点类Java 更改节点的值,java,Java,我目前有三个带有值的节点的LinkedStack。这些值是使用我在下面提到的类中创建的push方法创建的。我的问题是在我按下“1,2,3”键之后;我有一个包含3个元素的列表,在我的赋值中,需要使用while(current!=null)条件从顶部节点到底部按顺序更改这些元素等等。但我的问题是,我如何在不使用Arraylist库或此类方法的情况下更改元素值,这些方法用于在主类上使用setMethods简化此类命令。例如,这就是我所做的,但显然它不起作用,因为它只是指它本身,不改变任何东西。我所做的
公共类线性节点
{
专用线路节点下一步;//se guarda la direccion del Nodo
私有T元素;//Lista vacia
公共线性节点()
{
next=null;
元素=空;
}
//-----------------------------------------------------------------
//创建存储指定元素的节点。
//-----------------------------------------------------------------
公共线性节点(T元素,线性节点参考)
{
下一步=参考;
元素=元素;
}
//-----------------------------------------------------------------
//返回此节点后面的节点。
//-----------------------------------------------------------------
public LinearNode getNext()
{
下一步返回;
}
//-----------------------------------------------------------------
//设置此节点后面的节点。
//-----------------------------------------------------------------
public void setNext(LinearNode节点)
{
下一个=节点;
}
//-----------------------------------------------------------------
//返回存储在此节点中的元素。
//-----------------------------------------------------------------
public T getElement()//asigna valor
{
返回元素;
}
公共无效集合元素(T元素)
{
元素=元素;
}
}
LinkedStack类
公共类LinkedStack实现堆栈{
私人整数计数;
private LinearNode top;//参考节点(目录)
//-----------------------------------------------------------------
//使用默认容量创建空堆栈。
//-----------------------------------------------------------------
公共LinkedStack()
{
计数=0;
top=null;
}
//-----------------------------------------------------------------
//移除此堆栈顶部的元素并返回
//如果
//堆栈不包含任何元素。
//-----------------------------------------------------------------
公共LinearNode getLinearNode(){
返回顶部;
}
@凌驾
公共布尔值为空()
{
if(top==null)
{
System.out.println(“堆栈为空”);
}
返回top==null;
}
@凌驾
公共无效推送(T元素)
{
LinearNode电流=新的LinearNode(元素,顶部);
current.setNext(顶部);顶部是la变量dereferencea next
top=电流;
计数++;
}
@凌驾
公共广播电台
{
T结果;
System.out.println(“让我们弹出顶部元素!”);
如果(计数=0)
{
System.out.println(“堆栈为空”);
}
结果=top.getElement();
top=top.getNext();
计数--;
System.out.println(“我们弹出的元素是:“+””+“+result+””+“+”\n”);
返回结果;
}
@凌驾
公共字符串toString()
{
字符串结果=”;
线性节点电流=顶部;
系统输出打印(“+”\n”);
while(当前!=null)
{
结果+=“[”+当前.getElement()+“]”+“\n”;
current=current.getNext();
}
返回结果+“”+“\n”;
}
@凌驾
公共T Peek(){
System.out.println(“让我们看看最上面的元素!”);
如果(计数=0)
{
System.out.println(“Peek失败堆栈为空”);
}
System.out.println(“我们看到的元素是:“+”[“+top.getElement()+”]“+”\n”);
返回top.getElement();
}
@凌驾
公共整数大小(){
如果(计数!=0)
{
System.out.println(“让我们检查一下列表的大小!”);
System.out.println(“列表的大小为:“+”“+count+”“+”””+“\n”);
}
如果(计数=0)
{
System.out.println(“列表的大小是……哇。”);
System.out.println(“列表大小现在为:“+”“+count+”“+”“+”\n“+”推送更多元素!”);
}
返回计数;
}
}
主类
公共类LSmain{
公共静态void main(字符串[]args)
{
LinkedStack列表=新建LinkedStack();
System.out.println(“让我们列个清单吧!”);
System.out.println(“推3次”);
System.out.println(“检查大小”);
System.out.println(“查看顶部元素”);
System.out.println(“三次弹出”);
System.out.println(“现在的大小应该是零!”+“\n”);
列表。推送(1);
列表。推送(2);
列表。推送(3);
list.Size();
System.out.println(list.toString());
System.out.println(“更改值”);
#//#
其中,代码应更改列表中已推送的值的值
LinearNodecurrent;
当前=list.getLinearNode();
while(当前!=null)
{
当前。设置下一个(当前);
当前设置元素(5);
}
#//#
System.out.println(list.toString());
代码注释说明getLinearNode
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());
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();
}