Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中返回最小值的堆栈_Java - Fatal编程技术网

在Java中返回最小值的堆栈

在Java中返回最小值的堆栈,java,Java,我试图实现一个堆栈,它除了提供标准的push和pop之外,还返回O(1)time中的最小值 这是我的密码 import java.util.Comparator; import java.util.Iterator; import java.util.ListIterator; public class MinStack<T> { private Node head; private Node minHead; private T minValue;

我试图实现一个堆栈,它除了提供标准的push和pop之外,还返回
O(1)
time中的最小值

这是我的密码

import java.util.Comparator;
import java.util.Iterator;
import java.util.ListIterator;

public class MinStack<T> {

    private Node head;
    private Node minHead;
    private T minValue;


    private class Node<T extends Comparable<T>>  {
        private T data;
        private Node next;

        public Node(T data){
            this.data = data;
            this.next = null;
        }

        public int compareTo(T other){
            return data.compareTo(other);
        }

    }

    public void push(T item){
        Node p = new Node((Comparable) item);
        if(head == null){
            head = p;
            minHead = p;
            return;
        }
        p.next = head;
        head = p;

        if(((Comparable) item).compareTo(minValue) < 0){
            minValue = item;
            Node m = new Node((Comparable) item);
            m.next = minHead;
            minHead = m;
        }

    }

    public T pop(){
        if(head == null){
            System.out.println("Popping off an empty stack!!!");
            System.exit(-1);
        }
        Node item = (Node) head.data;
        if(item == minValue){
            minHead = minHead.next;
        }
        head = head.next;
        return (T) item;
    }

    public T getMin(){
        return minValue;
    }

    public void trace(){
        Node current = head;
        while(current != null){
            if(current.next == null){
              System.out.println(current.data);
            }else{
                System.out.println(current.data + "->");
            }
            current = current.next;
        }
    }

    public void minTrace(){
        Node current = minHead;
        while(current != null){
            if(current.next == null){
                System.out.println(current.data);
            }else{
                System.out.println(current.data + "->");
            }
            current = current.next;
        }
    }
}
import java.util.Comparator;
导入java.util.Iterator;
导入java.util.ListIterator;
公共课明斯塔克{
专用节点头;
私有节点minHead;
私有mint值;
私有类节点{
私有T数据;
私有节点下一步;
公共节点(T数据){
这个数据=数据;
this.next=null;
}
公共int比较(T其他){
返回数据。比较(其他);
}
}
公共无效推送(T项){
节点p=新节点((可比)项);
if(head==null){
水头=p;
minHead=p;
返回;
}
p、 下一个=头部;
水头=p;
如果((可比)项)。与(最小值)<0相比{
最小值=项目;
节点m=新节点((可比)项);
m、 next=minHead;
minHead=m;
}
}
公共广播电台{
if(head==null){
System.out.println(“弹出空堆栈!!!”;
系统退出(-1);
}
节点项=(节点)head.data;
如果(项==最小值){
minHead=minHead.next;
}
head=head.next;
退货(T)项;
}
公共T getMin(){
返回最小值;
}
公共无效跟踪(){
节点电流=头;
while(当前!=null){
if(current.next==null){
系统输出打印项次(当前数据);
}否则{
System.out.println(current.data+“->”);
}
当前=当前。下一步;
}
}
公开竞逐(){
节点电流=minHead;
while(当前!=null){
if(current.next==null){
系统输出打印项次(当前数据);
}否则{
System.out.println(current.data+“->”);
}
当前=当前。下一步;
}
}
}
当我使用以下客户端代码时

MinStack<Integer> stack = new MinStack<>();
        stack.push(12);
        stack.push(1);
        stack.push(7);
        stack.push(9);
        stack.push(3);
        stack.push(2);
        stack.trace();
MinStack堆栈=新的MinStack();
栈推(12);
堆栈推送(1);
栈推(7);
栈推(9);
堆栈推送(3);
栈推(2);
stack.trace();
我在使用compareTo函数比较T值的行中得到一个
空指针异常。有人能帮我理解我做错了什么吗

if(head == null){
            head = p;
            minHead = p;
            minValue = //try setting minvalue here
            return;
        }

当只有一个元素时,minValue将等于该元素。

您将获得
空指针异常
,因为您的
minValue
未初始化。在使用它之前,请尝试使用一些默认值进行初始化

此外,您的意图似乎是从数据结构中找到一个最小值。在这种情况下,
堆栈
不是一个好的解决方案。我建议您使用
优先级队列


如果您仍然使用Stack,也可能会对您有所帮助

是因为minValue没有初始化为任何值吗?当你弹出min值时会发生什么?