简体   繁体   English

链表堆栈实现-Java

[英]Linked List Stack Implementation - Java

I have to implement a stack in the form of Linked List for one of my assignments. 我必须为我的一项任务以链接列表的形式实现堆栈。 We have to implement the interface DStack , which has the methods push() , pop() , peek() , and isEmpty() in our LinkedList.java file. 我们必须实现接口DStack ,该接口在LinkedList.java文件中具有push()pop()peek()isEmpty() The list would take a series of doubles as its content. 该列表将包含一系列的双打内容。

What I am still struggling with is basically the push() and pop() methods for linked lists. 我仍然在挣扎的基本上是链表的push()pop()方法。 I am not sure about the nodes I have made, but here is the code that I have so far: 我不确定我已经建立了节点,但是这里是到目前为止的代码:

public class ListStack implements DStack{

private Node head;
private int size;


private class Node{
    double d;
    Node next;

    public Node(){
        head = null;
        next = null;
    }

    public Node(double data){
        d = data;
    }
}

public boolean isEmpty() {
    return (head == null);
}

@Override
public void push(double d) {
    if(!isEmpty()){
        Node newHead = new Node(d);
        newHead.next = head;
        head = newHead;
        size++;
    }
    else
        throw new EmptyStackException(); //To change body of generated methods, choose Tools | Templates.
}

@Override
public double pop() {
    if(!isEmpty()){
        double d = head.d;
        head = head.next;
        size--;
        return d;
    }
    else
        throw new EmptyStackException(); //To change body of generated methods, choose Tools | Templates.
}

@Override
public double peek() {
    if(!isEmpty()){
        return head.d;
    }
    else
        throw new EmptyStackException(); //To change body of generated methods, choose Tools | Templates.
}

Could someone advise me if I am doing the push() and pop() methods correctly? 有人可以建议我是否正确地执行push()pop()方法吗? If not, where exactly do I need to revise it? 如果没有,我到底需要在哪里修改它? I'm looking for explanations online but most of the materials don't really make sense to me. 我正在网上寻找解释,但是大多数材料对我来说真的没有意义。

Also, my teacher forbid us to use any other classes from the Java framework or other class library. 另外,我的老师禁止我们使用Java框架或其他类库中的任何其他类。 Does this mean I cannot use Iterable in my code? 这是否意味着我无法在代码中使用Iterable If so, then creating the additional methods for linked lists such as remove() , next() , etc. are not needed, right? 如果是这样,那么就不需要为链接列表创建其他方法,例如remove()next()等,对吗?

I'm still tripping over these linked lists concepts so if anyone could clear this up, I would really appreciate it. 我仍在探讨这些链表概念,因此,如果有人可以解决此问题,我将非常感激。 Thank you! 谢谢!

pop() and peek() look ok, but you shouldn't throw an exception when trying to push() on an empty stack. pop()peek()看起来不错,但是当尝试将push()放在空堆栈上时,您不应抛出异常。 You'll never be able to use the silly thing if that's the case. 如果是这样,您将永远无法使用这种愚蠢的东西。 Instead, you might want to do something different if it's empty, but under no conditions should push() throw that EmptyStackException . 相反,如果它为空,则可能要执行其他操作,但在任何情况下都不应push()抛出EmptyStackException

Looks OK, need to initialize head otherwise push/pop would always throws exception. 看起来还不错,需要初始化头,否则push / pop总是会抛出异常。 Consider to throw stack full exception when is not possible instead of empty stack exception. 考虑在不可能的情况下引发堆栈满异常,而不是空堆栈异常。 This would be unlimited list capacity, may be you could consider with a fixed capacity then there would be limitation on push. 这将是无限的列表容量,可能是您可以考虑使用固定容量,然后在推送上会有限制。

In addition to the already provided advise - it helped me a lot to draw the state of a stack/list for some basic scenarios: At the beginning, in between with a few elements in it and at the end when you delete the last element. 除了已经提供的建议外,它还为我在一些基本情况下绘制堆栈/列表的状态提供了很多帮助:开始时,介于其中的一些元素之间,以及结束时删除最后一个元素。 And then going through your code step by step and check what happens to the variables and pointers for push() and pop(). 然后逐步检查代码,检查push()和pop()的变量和指针发生了什么。

Of course that could also be done with the Debugger- but Pen & Paper sometimes clears things up ;) Good luck with your assignment! 当然,也可以使用调试器来完成,但是Pen&Paper有时可以解决问题;)祝您工作顺利!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM