'How to write `getMin()` method for a linked stack class that extends Comparable? (Homework)

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;
    }
    
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source