Java LinkedStack操作方法

Java LinkedStack操作方法,java,Java,我现在开始使用LinkedStacks,我想知道为什么toString和pop方法不起作用?我使用的方法是书中给出的默认方法,即Pop和toString方法,其余的方法我都使用过,并且运行良好。推送方法完美地添加了元素。peek在不改变列表和大小的情况下查看顶部元素,并返回我使用push方法的次数。奇怪的是,pop方法只工作一次,然后给出一个错误。请注意,本书在堆栈部分给出的toString方法的示例似乎也不起作用。我很乐意接受任何建议,但请知道我只是一个初学者,我正在学习。这是该类的代码: 代

我现在开始使用LinkedStacks,我想知道为什么toString和pop方法不起作用?我使用的方法是书中给出的默认方法,即Pop和toString方法,其余的方法我都使用过,并且运行良好。推送方法完美地添加了元素。peek在不改变列表和大小的情况下查看顶部元素,并返回我使用push方法的次数。奇怪的是,pop方法只工作一次,然后给出一个错误。请注意,本书在堆栈部分给出的toString方法的示例似乎也不起作用。我很乐意接受任何建议,但请知道我只是一个初学者,我正在学习。这是该类的代码:

代码 不使用pop方法输出两次 分析 /这实际上会创建一个新节点并存储插入参数中的值 错误在于我不能给同一个列表分配top,因为如果我这样做,列表将只由一个顶部节点组成。正如我过去的代码所说,我只是说顶部的砖块将等于它本身。所以我从类中了解到的是创建一个新的对象类型LinearNode来存储元素,然后top将等于新节点的值。在本例中,Pop将起作用,因为将有更多节点,而不是一个节点。关于toString方法的额外注意事项是,返回;在java中,有时会显示值,但大多数情况下并不意味着需要添加System.out.println();调用该方法时在驱动程序中,或改为在方法中。/

已更正推送方法:
@覆盖
公共无效推送(T元素)
{
LinearNode电流=新的LinearNode(元件);
当前设置下一步(顶部);
top=电流;
计数++;
}
主要类别代码:
公共类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);
System.out.println(list.toString());
list.Size();
list.Peek();
list.Pop();
list.Pop();
list.Pop();
list.Size();
}
功能输出
运行:
让我们列个清单吧!
推3次。
检查尺寸。
窥视顶部元素。
弹三下。
现在的大小应该是零!
[3][2][1]
让我们检查一下列表的大小!
列表的大小为:“3”
让我们来看看最上面的元素!
我们看到的元素是:[3]
让我们弹出顶部元素!
我们流行的元素是:“3”
让我们弹出顶部元素!
我们流行的元素是:“2”
让我们弹出顶部元素!
我们使用的元素是:“1”
名单的大小是…哇。
列表大小现在为:“0”
推动更多元素!
生成成功(总时间:3秒)
谢谢你的帮助! PS:我忘了将方法声明更改为camel case

线性节点码# /有不正确的参数,当我试图通过创建新的节点对象来纠正推送方法时出现了问题。/

包链接节点

public class LinearNode<T> {

    private LinearNode<T> next; //se guarda la referencia del Nodo
    private T element;          //Lista vacia

    public LinearNode()
{
next = null;
element = null;
}
//-----------------------------------------------------------------
// Creates a node storing the specified element.
//-----------------------------------------------------------------
public LinearNode (T elem)
{
next = null;
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;


}



}
公共类线性节点{
private LinearNode next;//se guarda la Referencea del Nodo
私有T元素;//Lista vacia
公共线性节点()
{
next=null;
元素=空;
}
//-----------------------------------------------------------------
//创建存储指定元素的节点。
//-----------------------------------------------------------------
公共线性节点(T元素)
{
next=null;
元素=元素;
}
//-----------------------------------------------------------------
//返回此节点后面的节点。
//-----------------------------------------------------------------
public LinearNode getNext()
{
下一步返回;
}
//-----------------------------------------------------------------
//设置此节点后面的节点。
//-----------------------------------------------------------------
public void setNext(LinearNode节点)
{
下一个=节点;
}
//-----------------------------------------------------------------
//返回存储在此节点中的元素。
//-----------------------------------------------------------------
public T getElement()//asigna valor
{
返回元素;
}
公共无效集合元素(T元素)
{
元素=元素;
}
}

这看起来像是一个问题:

if (count == 0) 
    {
    System.out.println("Pop operation failed. "+ "The stack is empty.");
    }

    result = top.getElement();  //NPE here perhaps
    top = top.getNext();  
    count--;
    System.out.println("The element that we have poped is :" + result);
    return result;
当您触发计数=0时,您不会从函数返回,而是继续处理pop操作。

  • Pop
    不起作用,因为您没有更新
    LinearNode
  • 即使堆栈为空,也会继续尝试弹出
  • 您没有解释为什么
    toString
    不起作用,它看起来没问题,但我会使用和
    .append()
    方法

你能告诉我哪一行是64吗?在java中,函数名是以小写字母开头的驼峰大小写。你能显示
LinearNode
的代码吗?这可能是适用的。你有没有尝试过用调试器逐步检查代码?我忘了显示LinearNode代码,它不正确。参数中有一个不必要的变量。下一步是e我将完整地显示该类,但LinearNode代码位于顶部。我更新了代码。感谢您的帮助。
“如何更新LinearNode中的内部引用?”
mean?谢谢你的指针,但我确实提到了toString方法在我调用它时没有显示任何类型的输出。这看起来确实是个问题,但他推了3次,然后又弹出了两次。
count
没有意义!现在我注意到输出是空的堆栈。但问题是什么是NPE?@jorgavego NPE==Nul除了你,先生,还有当我推我的时候
1
Exception in thread "main" java.lang.NullPointerException
2
3
The size of the list now is: 3
Lets peek the top element!
The element that we have peeked is: 3
Lets pop the top element!
The element that we have poped is: 3
Lets pop the top element!
    at LinkNod.LinkedStack.Pop(LinkedStack.java:64)
    at LinkNod.LSmain.main(LSmain.java:22)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
run:
1
2
3
The size of the list now is: 3
Lets peek the top element!
The element that we have peeked is: 3
Lets pop the top element!
The element that we have poped is: 3
The size of the list now is: 2
@Override
    public void Push(T element)
{

     LinearNode<T> current = new LinearNode<>(element); 

     current.setNext(top);
     top = current;
     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);
     System.out.println(list.toString());
     list.Size();
     list.Peek();
     list.Pop();
     list.Pop();
     list.Pop();
     list.Size();



    }
run:
Let's make a List!
Push 3 times.
Check the size.
Peek the top element.
Pop three times.
The size now should be zero!

<top of stack-->[3][2][1]<--bottom of stack>

Let's check the size of the list!
The size of the list is: '3'

Lets peek the top element!
The element that we have peeked is: [3]

Lets pop the top element!
The element that we have poped is: '3'

Lets pop the top element!
The element that we have poped is: '2'

Lets pop the top element!
The element that we have poped is: '1'

The size of the list is...Woah.
The list size is now: '0'
Push more elements!
BUILD SUCCESSFUL (total time: 3 seconds)
public class LinearNode<T> {

    private LinearNode<T> next; //se guarda la referencia del Nodo
    private T element;          //Lista vacia

    public LinearNode()
{
next = null;
element = null;
}
//-----------------------------------------------------------------
// Creates a node storing the specified element.
//-----------------------------------------------------------------
public LinearNode (T elem)
{
next = null;
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;


}



}
if (count == 0) 
    {
    System.out.println("Pop operation failed. "+ "The stack is empty.");
    }

    result = top.getElement();  //NPE here perhaps
    top = top.getNext();  
    count--;
    System.out.println("The element that we have poped is :" + result);
    return result;