什么是小堆栈,小堆栈如何排序??
大家可否将代码贴出,或发代码到:[email protected]

解决方案 »

  1.   

    本人写的栈,经过测试比sun的API的Stack类快,因为sun的Stack是继承Vector
    使用数组实现的栈,而我用链表实现栈。所以比它快
     
    public class Stack{
    private Element current;
    public void push(Object o){
    Element e=current;
    current=new Element();
    current.previous=e;
    current.object=o;
    }
    public Object pop(){
    if(current==null){
    return null;
    }
    Object o=current.object;
    current=current.previous;
    return o;
    }
    private class Element{
    Element previous;
    Object object;
    }
    }由于你又说需要排序的栈,经过我仔细思考,估计你的意思是在把数据插入栈
    的时候进行排序,所以我又把代码修改一下改成push进去的时候排序,不知道
    是不是符合你的要求?public class SortedStack {
    private Element current=null;
    public void push(Integer o){
    Element e=current;
    //如果栈里没有数据,或者栈里的数据比放入的元素小,那就直接把元素放在栈的最后
    if(e==null||e.object.compareTo(o)<=0){
    current=new Element();
    current.previous=e;
    current.object=o;
    return;
    }
    //如果栈的节点的元素比放入的元素大,或者遍历到前方还有数据
    while(e.previous!=null&&e.previous.object.compareTo(o)>0){
    e=e.previous;
    }
    Element node=new Element();
    node.object=o;
    node.previous=e.previous;
    e.previous=node;
    } public Integer pop(){
    if(current==null){
    return null;
    }
    Integer o=current.object;
    current=current.previous;
    return o;
    }
    private class Element{
    Element previous;
    Integer object;
    }
    }
      

  2.   

    楼主,以我搞过堆溢出攻击的经验来看,依然不知道什么是小堆栈,
    于是我baidu了一下,发现这个词汇的search结果,第一条是一个人的blog,他用一个小字,来修饰堆栈,有装嫩的嫌疑。
    而第二条就是你这个帖子了,所以我怀疑这个词汇是你杜撰出来的 所以不要再执着了,你听谁说的啊?