LinkedStack操作方法

时间:2013-04-08 12:20:42

标签: java

我目前正在使用LinkedStacks,我想知道为什么toString和pop方法不起作用?我使用的方法是本书给出的默认方法,即Pop和toString方法,其余的我工作并且运行良好。推送方法完美地添加元素。窥视顶部元素而不改变List和Size,返回我使用push方法的次数。 pop方法奇怪地只工作一次然后发出错误。注意本书在Stacks部分给出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;
    }


    }

主类代码:

public class 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

分析

/ 这实际上会创建一个新节点并存储插入参数中的值 错误是我不能给同一个列表赋予最高分配,因为如果我这样做,List将只由一个顶部的Node组成。就像我过去的代码所说的那样,我只是说顶砖会与自己相等。所以我从类中理解的是创建一个新的对象类型LinearNode,然后存储元素,然后top将等于新Node的值。由于这种情况,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:我忘了将方法声明更改为驼峰案例。

LinearNode代码#

/ 当我尝试通过创建新的Node对象来纠正push方法时,出现了不正确的参数。 /

包LinkNod;

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;


}



}

2 个答案:

答案 0 :(得分: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时,您不会从函数返回,而是继续处理弹出操作。

答案 1 :(得分:0)

  • Pop不起作用,因为您没有更新LinearNode
  • 中的内部参考
  • 即使堆栈为空,您也会继续尝试弹出
  • 您没有解释为什么toString不起作用,看起来没问题,但我会使用StringBuilder.append()方法。
相关问题