0

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

代码

public class LinkedStack<T> implements Stack<T> {



private int count;
private LinearNode<T> top;

//-----------------------------------------------------------------
// Creates an empty stack using the default capacity.
//-----------------------------------------------------------------
public LinkedStack()
{
count = 0;
top = null;
}


    @Override
    public boolean IsEmpty()

    {
        if(top == null)
        {
        System.out.println("Stack is empty");
        }
        return top == null;
    }

    @Override
    public void Push(T element)
{

    top = new LinearNode(element, top);
    System.out.println(element);
    count++;


}

    @Override 
    public T Pop() 
{
    T result;
    System.out.println("Lets pop the top element!");

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

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

}

    Override
    public String toString()
{

    String result = "<top of stack>\n";
LinearNode current = top;
while (current != null)
{
result += current.getElement() + "\n";
current = current.getNext();
}
return result + "<bottom of stack>";

}

    @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());
        return top.getElement();

    }

    @Override
    public int Size() {
        System.out.println("The size of the list now is: " + count);
        return count;
    }


    }

主类代码:

公共类 LSmain {

public static void main(String[]args)
{
 LinkedStack<Integer> main = new LinkedStack<>();   
 main.Push(1);
 main.Push(2);
 main.Push(3);
 main.Size();
 main.Peek();
 main.Pop();
 main.Pop();
 main.Size();
 main.toString();


}

}

使用pop方法输出两次

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)

输出不使用两次pop方法

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

分析

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

推送方法更正:

@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)

谢谢您的帮助!PS:我忘了将方法声明更改为驼峰式。

线性节点代码#

/当我试图通过创建一个新的 Node 对象来纠正 push 方法时,参数不正确会带来麻烦。/

包链接节点;

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;


}



}
4

2 回答 2

0

这看起来像问题:

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;

当您触发时,count == 0您不会从函数返回,而是继续处理弹出操作。

于 2013-04-08T12:30:58.900 回答
0
  • Pop不起作用,因为您没有更新内部引用LinearNode
  • 即使堆栈为空,您也会继续尝试弹出
  • 您没有解释为什么toString不起作用,看起来还可以,但我会使用StringBuilder这些.append()方法。
于 2013-04-08T12:31:05.967 回答