JAVA从零开始实现数据结构三:栈

完整的MyStack类
/**
 * Created by FireFlies on 2018/4/3.
 *clearStack(): 将栈清空。
 *isStackEmpty(): 若栈为空, 返回true, 否则返回false。
 *getTop(): 若栈存在且非空, 用e返回S的栈顶元素。
 *push(E e): 若栈存在, 插入新元素e到栈S中并成为栈顶元素。
 *pop(): 删除栈中栈顶元素, 并用e返回其值。
 *stackLength(): 返回栈的元素个数。
 */
public class MyStack<E> {
    Object[] data = null;
    int capacity;
    int current;

    public MyStack(){
        this(10);
    }

    public MyStack(int initialSize){
        data = new Object[initialSize];
        capacity = initialSize;
        current = 0;
    }

    /**
     *进栈操作push
     * 类似于顺序表的add操作,在尾部加入元素
     */
    public boolean push(E e){
        ensureSize();
        data[current++] = e;
        return true;
    }

    /**
     * 出栈操作pop
     * 删除尾部元素
     */
    public E pop(){
        if(current<=0)
            throw new RuntimeException("栈为空");

        return (E) data[--current];
    }

    /**
     * 若栈存在且非空, 用e返回栈顶元素。
     */
    public E getTop(){
        if(current==0)
            throw new RuntimeException("栈为空");
        return (E) data[current-1];
    }

    /**
     * 返回栈的元素个数。
     * @return
     */
    public int stackLength() {
        return this.current;
    }

    /**
     * 若栈为空, 返回true, 否则返回false。
     */
    public boolean isStackEmpty(){
        if(current=0)
            return true;
        return false;
    }

    /**
     * 将栈清空。
     */
    public boolean clearStack(){
        current = 0;
        return true;
    }

    /**
     * 获取指定index的元素
     * @return
     */
    public E get(int index) {
        validateIndex(index);
        return (E) data[index];
    }

    /**
     *  验证当前下标是否合法,如果不合法,抛出运行时异常
     * @param index 下标
     */
    private void validateIndex(int index) {
        if (index < 0 || index > current) {
            throw new RuntimeException("数组index错误:" + index);
        }
    }

    /**
     * 实现数组动态扩容
     */
    public void ensureSize(){
        if(current == capacity){
            capacity *= 2;
            Object[] temp = new Object[capacity];
            for(int i=0; i<current; i++){
                temp[i] = data[i];
            }
            data = temp;
        }
    }

    /**
     * 输出当前线性表所有元素
     */
    public void print(){
        System.out.print("[");
        for(int i=0;i<this.stackLength();i++){
            System.out.print(this.get(i)+" ");
        }
        System.out.println("]");
    }

}
测试类
/**
 * Created by FireFlies on 2018/4/3.
 */
public class MyStackTest {
    public static void main(String[] args) {
        MyStack myStack = new MyStack(3);

        //测试进栈操作
        System.out.println("测试进栈操作:");
        myStack.push(new Integer(1));
        myStack.push(new Integer(2));
        myStack.push(new Integer(3));
        myStack.print();

        //测试getTop
        System.out.println("查看栈顶元素");
        System.out.println("Stack top: "+myStack.getTop().toString());


        //测试出栈操作
        System.out.println("测试出栈操作:");
        System.out.println("出栈元素:"+myStack.pop().toString());
        myStack.print();
        System.out.println("出栈元素:"+myStack.pop().toString());
        myStack.print();

        //测试清空操作
        System.out.println("测试清空操作");
        myStack.clearStack();
        System.out.println("Stack empty: "+myStack.isStackEmpty());
    }
}
输出结果
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,768评论 19 139
  • 文章来自:http://blog.csdn.net/mj813/article/details/52451355 ...
    好大一只鹏阅读 13,010评论 2 126
  • 1.测试与软件模型 软件开发生命周期模型指的是软件开发全过程、活动和任务的结构性框架。软件项目的开发包括:需求、设...
    Mr希灵阅读 22,240评论 7 278
  • 很多人说python语言运行速度慢,那么我用一个遍历图片像素的例子做对比。 准备工作 一张2048x1024大小的...
    逆风g阅读 14,848评论 0 3
  • 差不多又一年没见田里的烟火了。记得有人说过:宁愿跌进人间的烟火里,也不愿意在天堂里观尽烟火。可见人间烟火的对于我们...
    洪七公叫花鸡阅读 3,269评论 0 1