0

我目前有三个带有值的节点的 LinkedStack。这些值是使用我在下面提到的类中创建的 push 方法创建的。我的问题是在我按下 :"1,2,3"; 之后 我有一个 3 个元素的列表,在我的分配中需要使用条件 while(current != 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;


    }



    }

LinkedStack 类

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());
4

1 回答 1

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();
}
于 2013-04-14T05:17:57.727 回答