如何为扩展 Comparable 的链接堆栈类编写 getMin() 方法? (家庭作业)

发布于 2025-01-11 09:22:25 字数 2157 浏览 1 评论 0 原文

对于此作业,我需要创建一个包含 getMin()getMax() 的链接堆栈类。我无法更改讲师提供的类标题。 getMingetMax 时间都应该是 O(1)。

我的想法是,我需要使用 compareTo 方法在推送或弹出条目时比较条目,以便我可以设置变量 minValuemaxValue等于它们各自的值。但是,我不明白类头 > 我也不知道如何或在哪里实现 Comparable。我尝试让我的 class Node 实现 Comparable 但它要求我重写 compareTo 方法,我不确定这会如何工作。

任何帮助将不胜感激!以下是我针对此作业的代码:

public class MinMaxStack <T extends Comparable<? super T>> implements StackADT<T> { 
    private Node<T> top;
    private int size;
    
    public MinMaxStack() {
        clear();
    }
    
    private class Node<E>{
        E data;
        Node<E> previous;
    }
    
    public T getMin() {     
        if(isEmpty()) {
            throw new EmptyCollectionException("The stack is empty but is trying to getMin.");
        } else {
            return null;
        }
    }
    
    public T getMax() {
        return null;
    }
    
    @Override
    public T pop() {
        if(isEmpty()) {
            throw new EmptyStackException("Stack is empty but trying to pop.");
        }else {
            T dataToReturn = top.data;
            top = top.previous;
            size -= 1;
            return dataToReturn;
        }
    }

    @Override
    public T peek() {
        if(isEmpty()) {
            throw new EmptyStackException("Stack is empty but trying to peek");
        }else {
            return top.data;
        }
    }

    @Override
    public void push(T newItem) {
        Node<T> newNode = new Node<>();
        newNode.data = newItem;
        if(!isEmpty()) {
        newNode.previous = top;
        }
        top = newNode;
        size += 1; 
    }

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

    @Override
    public void clear() {
        while(!isEmpty()) {
            top = null;
            size = 0;
        }
        
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }
    
}

For this assignment I need to create a linked stack class that contains a getMin() and getMax(). I cannot change the class header which was provided by the instructor. Both getMin and getMax should be O(1) time.

My thought is that I need to use the compareTo method to compare entries as they are pushed or poped so that I can set variables minValue and maxValue equal to their respective values. However, I don't understand the section in the class header <T extends Comparable<? super T>> nor do I know how or where to implement Comparable. I tried having my class Node<E> implement Comparable but it asked me to override the compareTo method and I'm not sure how that would work.

Any help would be greatly appreciated! Below is my code for this assignment:

public class MinMaxStack <T extends Comparable<? super T>> implements StackADT<T> { 
    private Node<T> top;
    private int size;
    
    public MinMaxStack() {
        clear();
    }
    
    private class Node<E>{
        E data;
        Node<E> previous;
    }
    
    public T getMin() {     
        if(isEmpty()) {
            throw new EmptyCollectionException("The stack is empty but is trying to getMin.");
        } else {
            return null;
        }
    }
    
    public T getMax() {
        return null;
    }
    
    @Override
    public T pop() {
        if(isEmpty()) {
            throw new EmptyStackException("Stack is empty but trying to pop.");
        }else {
            T dataToReturn = top.data;
            top = top.previous;
            size -= 1;
            return dataToReturn;
        }
    }

    @Override
    public T peek() {
        if(isEmpty()) {
            throw new EmptyStackException("Stack is empty but trying to peek");
        }else {
            return top.data;
        }
    }

    @Override
    public void push(T newItem) {
        Node<T> newNode = new Node<>();
        newNode.data = newItem;
        if(!isEmpty()) {
        newNode.previous = top;
        }
        top = newNode;
        size += 1; 
    }

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

    @Override
    public void clear() {
        while(!isEmpty()) {
            top = null;
            size = 0;
        }
        
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文