java 如何为链表编写 peek 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7679326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:56:41  来源:igfitidea点击:

How to code a peek method for Linked List

javastackpeek

提问by Brock

I am creating a linked list implementation for a stack. I've got the pop and push method done, but I can't seem to get the peek methods right. The code I have now in there, returns the memory address I think.

我正在为堆栈创建一个链表实现。我已经完成了 pop 和 push 方法,但我似乎无法正确使用 peek 方法。我现在在那里的代码,返回我认为的内存地址。

Here is my code:

这是我的代码:

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

private int count;
private LinearNode<T> contents;

public LinkedStack() {
    count = 0;
    contents = null;
}

@Override
public void push(T element) {
    LinearNode<T> top = new LinearNode<T>(element);

    if (contents == null) {
        contents = top;
    } else {
        LinearNode<T> current = contents;

        while (current.getNext() != null) {
            current = current.getNext();
        }
        current.setNext(top);
    }
    count++;
}

@SuppressWarnings("unchecked")
@Override
public T pop() {
    T item = (T) contents;
    contents = contents.getNext();
    count--;
    return item;
}

@SuppressWarnings("unchecked")
@Override
public T peek() throws NoSuchOperationException {

    T top = (T) contents;
    if(top == null){
        throw new NoSuchOperationException();
    }
    return top;
}

@Override
public boolean isEmpty() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public int size() {
    return count;
}
}

This is what it outputs when I call the peek method. I used my push method to add an object, and I tested it with the size method. It showed that I added an element. Then I called my pop method and displayed the size again to make sure that worked.

这是我调用 peek 方法时输出的内容。我用我的push方法添加了一个对象,我用size方法测试了它。它表明我添加了一个元素。然后我调用我的 pop 方法并再次显示大小以确保它有效。

This is my output of the peek method:

这是我的 peek 方法的输出:

LinearNode@33f42b49

线性节点@33f42b49

Here is my LinearNode class:

这是我的 LinearNode 类:

public class LinearNode<T> {
private T element;
private LinearNode<T> next;

public LinearNode() {
    this.element = null;
    this.next = null;
}
public LinearNode(T element) {
    this.element = element;
    this.next = null;
}
public T getElement() {
    return element;
}
public void setElement(T _element) {
    this.element = _element;
}
public LinearNode<T> getNext() {
    return next;
}
public void setNext(LinearNode<T> next) {
    this.next = next;
}

}

}

回答by Jim Kiley

Sounds like the LinearNode class needs a toString() method.

听起来 LinearNode 类需要一个 toString() 方法。

回答by KarlP

pop() and peek() should return what the LinearNode references, not the actual LinkedNode.

pop() 和 peek() 应该返回 LinearNode 引用的内容,而不是实际的 LinkedNode。

The @SupressWarnings indicate that you are not doing generics right. Remove them, and look at what you are returning - it does not look like you are returning a `T' but the LinkedNode directly. You should not need any SupressWarnings in this code.

@SupressWarnings 表明您没有正确使用泛型。删除它们,然后查看您返回的内容 - 看起来您返回的不是“T”而是直接返回 LinkedNode。您不应该在此代码中需要任何 SupressWarnings。

If you want to store Strings then you should define the stack like this:

如果你想存储字符串,那么你应该像这样定义堆栈:

LinkedStack<String> stack = new LinkedStack<String>();

You can think that the generic type Twill then represent a String inside your stack class.

您可以认为泛型类型T将在您的堆栈类中表示一个字符串。

In that case, the peek()methods signature will then return a String.

在这种情况下,peek()方法签名将返回一个字符串。

Also: Check your pop and push methods, both should be able to do in O(1), however, you are traversing the list. You have got that backwards.

另外:检查您的 pop 和 push 方法,两者都应该能够在 O(1) 中完成,但是,您正在遍历列表。你已经倒退了。

回答by Jorge A Vega Vigo

You can simple peek by using: System.out.println("The element on top is: " + top.getElement); and return top.getElement. Remember in top you are actually removing a Node from the top of the stack and its element. Since peek only actually sees the top element of the stack you don't need to use top = top.getNext or count--;. Hope it helps!

您可以使用以下方法简单查看: System.out.println("The element on top is: " + top.getElement); 并返回 top.getElement。请记住,在 top 中,您实际上是从堆栈及其元素的顶部删除一个节点。由于 peek 实际上只看到堆栈的顶部元素,因此您不需要使用 top = top.getNext 或 count--;。希望能帮助到你!