美文网首页
手动撸一个基于数组的Stack

手动撸一个基于数组的Stack

作者: HWilliamgo | 来源:发表于2018-02-05 20:36 被阅读3次

public class MyStack<T> {
    private T t;
    private int size;
    private T[] items;
    private int current;
    private static final int DEFAUL_CAPACITY = 10;
    //构造函数清除并确认stack初始容量为10
    public MyStack() {
        clear();
        ensureCapacity(DEFAUL_CAPACITY);
    }
    //将stack的容量剪裁到size大小
    public void trimToSize() {
        ensureCapacity(size());
    }

    public int size() {
        return size;
    }

    //清空栈,并让指针指到-1的地方。
    public void clear() {
        this.size = 0;
        current = -1;
    }

    /**
     * 重新确认stack的容量,并将stack中数据拷贝。
     * @param newCapacity 新的容量
     */
    public void ensureCapacity(int newCapacity) {
        if (newCapacity < size) {
            System.out.println("新的容量小于已存在的数组的容量");
            return;
        }
        T[] oldItems = items;
        items = (T[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            items[i] = oldItems[i];
        }
    }

    public boolean isEmpty() throws Exception {
        if (current == 0) {
            return true;
        } else if (current > 0) {
            return false;
        } else {
            throw new Exception("the pointer of the stack is below 0");
        }
    }

    public boolean isFull() throws Exception {
        if (current > 0) {
            return current == size() - 1;
        } else {
            return false;
        }
    }

    public T pop() throws Exception {
        if (isEmpty()) {
            throw new Exception("the stack is empty now");
        }
        size--;
        return items[current--];
    }

    public T top() throws Exception {
        if(isEmpty()){
            throw new Exception("the stack is empty now");
        }
        return items[current];
    }
    public void push(T x) throws Exception {
        if (isFull()) {
            ensureCapacity(size() * 2);
        }
        items[++current] = x;
        size++;
    }
}

入口函数测试:

public class Main {
    public static void main(String []args) throws Exception {
        MyStack<Integer> stack=new MyStack<>();
        stack.push(10);
        stack.push(9);
        stack.push(8);
        stack.push(7);
        stack.push(6);
        stack.push(5);
        stack.push(4);
        stack.push(3);
        stack.push(2);
        stack.push(1);
        stack.push(0);
        stack.push(-1);
        stack.push(-2);
        System.out.println("The size of the stack is "+stack.size());
        int a=stack.pop();
        System.out.println("The size of the stack is "+stack.size());
        int b=stack.top();
        System.out.println("a ="+a);
        System.out.println("The top of the stack is "+b);
    }
}

打印:

The size of the stack is 13
The size of the stack is 12
a =-2
The top of the stack is -1

相关文章

  • 手动撸一个基于数组的Stack

    入口函数测试: 打印:

  • 栈实现

    栈是先进先出数据结构。下面基于数组,实现栈结构。 Stack继承Vector elementData:Object...

  • 数据结构之栈和队列

    栈 栈 Stack: 栈是一种线性结构 相比数组,栈对应的操作是数组的子集,所以我们完全可以基于动态数组去实现它 ...

  • Erlang数据结构

    Stack Queue Table Trees Graph Union/Find Stack 基于单个List的实...

  • stack实现

    stack是一种后进先出的数据结构。 stack的顺序写法(数组) stack的链接表示(链表)

  • Flutter的异常打印信息过滤,快速定位代码异常

    项目地址 基于dart官方stack_trace开发 stack_trace打印 flutter_stack_tr...

  • 手动撸一个AsyncTask

    前言:很多时候都需要用线程来操作耗时操作,为了更加深入的了解一下AsyncTask的工作原理,决定手动撸一个Asy...

  • Go语言编程--笔记2018-04-10

    书中35页谈到基于数组切片创建数组切片的时候 4. 基于数组切片创建数组切片类似于数组切片可以基于一个数组创建,数...

  • 2018-12-28

    基于常见数据结构整理 数据结构 数据存储的常用结构有:栈、队列、数组、链表和红黑树 栈 1.stack,又称堆栈,...

  • 数组实现stack

    在 Java 中 Stack 类表示后进先出(LIFO)的对象堆栈。栈是一种非常常见的数据结构,它采用典型的先进后...

网友评论

      本文标题:手动撸一个基于数组的Stack

      本文链接:https://www.haomeiwen.com/subject/ynwizxtx.html