如何为链表编写 peek 方法
我正在为堆栈创建一个链表实现。我已经完成了 pop 和 push 方法,但我似乎无法正确使用 peek 方法。我现在在那里的代码返回我认为的内存地址。
这是我的代码:
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;
}
}
这是我调用 peek 方法时输出的内容。我使用了 Push 方法来添加对象,并使用 size 方法对其进行了测试。它表明我添加了一个元素。然后我调用 pop 方法并再次显示大小以确保其有效。
这是我的 peek 方法的输出:
LinearNode@33f42b49
这是我的 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;
}
}
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.
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.
This is my output of the peek method:
LinearNode@33f42b49
Here is my LinearNode class:
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来 LinearNode 类需要一个 toString() 方法。
Sounds like the LinearNode class needs a toString() method.
pop() 和 peek() 应该返回 LinearNode 引用的内容,而不是实际的 LinkedNode。
@SupressWarnings 表明您没有正确执行泛型。删除它们,然后看看您返回的内容 - 看起来您返回的不是“T”,而是直接返回的 LinkedNode。您不应在此代码中需要任何 SupressWarnings。
如果你想存储字符串,那么你应该像这样定义堆栈:
你可以认为泛型类型
T
将代表堆栈类中的字符串。在这种情况下,
peek()
方法签名将返回一个字符串。另外:检查您的 pop 和 push 方法,两者都应该能够在 O(1) 内完成,但是,您正在遍历列表。你已经搞反了。
pop() and peek() should return what the LinearNode references, not the actual 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.
If you want to store Strings then you should define the stack like this:
You can think that the generic type
T
will then represent a String inside your stack class.In that case, the
peek()
methods signature will then return a String.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.
您可以使用以下方法简单地查看: System.out.println("顶部的元素是:" + top.getElement);
并返回top.getElement。请记住,在 top 中,您实际上是从堆栈顶部及其元素中删除一个节点。由于 peek 实际上只看到堆栈的顶部元素,因此您不需要使用 top = top.getNext 或 count--; 。希望有帮助!
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!