Stack implementation in java

Stack structure is a limited linear operating table structure, which means that it is implemented inside the array, but some special operation. Last-out stack feature is only operational in the top of the stack elements, like a base sealed bottle, the bottle to put things down, the first to go are pressed at the bottom, only to finally come out, and we stack structure is also in this way. Stack structure is realized List interface, so our List interface design, please refer to an article in
java ArrayList achieve
more than just design the interface of the same, in addition to the implementation class subclass-specific methods to achieve several other methods and, like all previous blog. Paste the code below:

package com.yxc.util;

import java.util.Iterator;

/**
 * 栈的实现
 * 其实我们把栈看成是有特殊操作的集合,只操作栈顶元素的一种结构。
 * 内部同样可以使用数组去实现,我们假定栈顶元素就是数组下标为0位置的元素
 * 实现MyList,只是在这个类中多了几个操作 其它的都和MyArrayList实现一样的
 * @param <E>
 */
public class MyStack<E> implements MyList<E>{
    //定义数组
    private E[] elements;
    //定义初始容量
    private int initCapacity;
    //默认扩宽长度
    private int entendsCapacity;
    //集合的长度
    private int size=0;

    //下面通过构造函数链来初始化
    //如果无参构造,那我们初始化一些值
    public MyStack(){
        this(10,10);
    }
    //只给点初始容量,那我们默认扩容10
    public MyStack(int initCapacity){
        //调用自己的构造方法
        this(initCapacity,10);

    }
    public MyStack(int initCapacity,int entendsCapacity){
        //判断一下参数的合法性
        if(initCapacity<0||entendsCapacity<0){
            //自己抛异常还是很爽的,可以自己写异常,然后抛出
            throw new IllegalArgumentException("非法参数");
        }
        elements=(E[])new Object[initCapacity];
        this.entendsCapacity=entendsCapacity;
    }

    //扩容方法
    public void ensureCapacity(int newCapacity){
        //判断容量是不是正确
        if(elements.length>newCapacity){
           return;
        }
        //将数组保留下来
        E[] oldElements=elements;
        //创建一个新的数组
        E[] newElement=(E[]) new Object[newCapacity];
        //将旧数组的元素复制到新数组中
        System.arraycopy(oldElements,0,newCapacity,0,size);
        //将新数组的引用给原数组
        elements=newElement;
        //将没用的数组引用释放
        oldElements=null;
    }

    //检查下标
    public void checkIndex(int index){
        if(index<0||index>size){
            throw new IndexOutOfBoundsException("数组下标越界,请检查下标"+index);
        }
    }

    /**
     * 入栈操作 也就是将元素插入到栈顶
     */
    public void push(E elem){
        //判断栈是不是满了 满了就要扩容 但是我们在add方法里面就有扩容这一步,所以我们这边就不写
        //对于入栈,不就是在栈顶元素添加元素,栈顶元素就是下标为0的位置
        add(0,elem);
    }

    /**
     * 返回栈顶元素,并且删除 也就是出栈
     */
    public E pop(){
        //先保留元素
        E temp=elements[0];
        //然后删除元素
        remove(0);
        return temp;
    }

    /**
     * 返回栈顶元素,但是不删除栈顶元素
     */
    public E peek(){
        //返回栈顶元素
        return elements[0];
    }
    @Override
    public void add(E elem) {
         add(0,elem);
    }

    @Override
    public void add(int index, E elem) {
        checkIndex(index);
        //判断是不是要扩容
        if(elements.length==size){
            ensureCapacity(size+entendsCapacity);
        }
        //移动元素 赋值  自增
        System.arraycopy(elements,index,elements,index+1,size-index);
        elements[index]=elem;
        size++;
    }

    @Override
    public E get(int index) {
        return null;
    }

    @Override
    public E set(int index, E e) {
        return null;
    }

    @Override
    public int indexOf(E elem) {
        return 0;
    }

    @Override
    public int lastIndexOf(E elem) {
        return 0;
    }

    @Override
    public boolean Contain(E e) {
        return false;
    }

    @Override
    public E remove(int index) {
        checkIndex(index);
        //保留数据
        E temp=elements[index];
        //复制元素
        System.arraycopy(elements,index+1,elements,index,size-index-1);
        size--;
        return temp;
    }

    @Override
    public boolean remove(E elem) {
        return false;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void clear() {
        size=0;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public void trimToSize() {

    }

    @Override
    public Iterator<E> iterator() {
        return null;
    }
}

Explain the method does not implement reference on a blog post, link up above.

Published 33 original articles · won praise 37 · views 4395

Guess you like

Origin blog.csdn.net/weixin_42142899/article/details/102052507