Java实现栈Stack_栈内部使用数组存储结构

Java实现栈Stack_栈内部使用数组存储结构

抽象数据类型栈的定义:

栈(stack),是限定在表尾进行插入或删除操作的线性表,因此对栈来说表尾有其特殊的含义,称为栈顶,相应的,表头端称为栈底。不含元素的空表称为空栈。

栈-后进先出(last in first out)

栈-顺序栈-栈的顺序存储结构是利用一组地址连续的存储单元依次存放自栈底到栈顶的数据元素

 

具体可参考java.util.Stack;内部已实现的数据结构栈,这里只是为了体会Java中高级数据结构的实现过程。

贴代码

package hash;

/**
 * Created with IntelliJ IDEA.
 * User: ASUS
 * Date: 14-9-14
 * Time: 下午7:14
 * To change this template use File | Settings | File Templates.
 */
public class CustomStack<E> {

    protected E[] elementData;  //栈内部使用数组来存储
    protected int elementCount; // 栈当前的元素数量
    protected int capacityIncrement;  //容量增长
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

    public CustomStack(int initialCapacity, int capacityIncrement) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
        this.elementData = (E[]) new Object[initialCapacity];
        this.capacityIncrement = capacityIncrement;
    }

    /**
     * 元素入栈
     * 同步方法
     * 这也就是同步方法,他锁定的是调用这个同步方法对象。
     * 也就是说,当一个对象P在不同的线程中执行这个同步方法时,他们之间会形成互斥,达到同步的效果。
     */
    public synchronized E push(E item) {
        //当栈容量已满时,扩大栈容量
        int minCapacity = elementCount + 1;
        if (minCapacity - elementData.length > 0) { //说明当前栈已满
            int oldCapacity = elementData.length;
            int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity);
            if (newCapacity - minCapacity < 0) {
                newCapacity = minCapacity;
            }
            if (newCapacity - MAX_ARRAY_SIZE > 0) {
                newCapacity = (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE;
            }
            E[] copy = (E[]) new Object[newCapacity]; //新的数组
            System.arraycopy(elementData, 0, copy, 0, elementData.length);  //数组的拷贝
            elementData = copy;
        }
        elementData[elementCount++] = item;
        return item;
    }

    /**
     * 栈顶元素出栈,同时移除该元素
     */
    public synchronized E pop() throws Exception {
        E obj;
        int index = elementCount - 1; //最后一个元素的索引
        if (index < 0) {
            throw new Exception("数组越界");
        }
        obj = elementData[index];
        elementData[index] = null; //同时移除元素gc自动清除内存垃圾
        elementCount--;
        return obj;
    }

    /**
     * 栈顶元素出栈,而不从栈中移除改元素
     */
    public synchronized E peek() throws Exception {
        E obj;
        int index = elementCount - 1; //最后一个元素的索引
        if (index < 0) {
            throw new Exception("数组越界");
        }
        obj = elementData[index];
        return obj;
    }

    /**
     * 栈是否为空
     */
    public synchronized boolean empty() {
        return elementCount == 0;
    }


    /**
     * 返回的是栈这种数据结构中的索引
     * 不是数组的索引
     * 直接遍历栈内部存储结构数组,定位元素
     *
     * @param o
     * @return
     */
    public synchronized int search(Object o) {
        int index = elementCount - 1;
        if (o == null) {
            for (int i = index; i >= 0; i--) {
                if (elementData[index] == null) {
                    return elementCount - i;
                }
            }
        } else {
            for (int i = index; i >= 0; i--) {
                if (elementData[index].equals(o)) {
                    return elementCount - i;
                }
            }
        }
        return -1;
    }

    public static void main(String args[]) throws Exception {
        CustomStack<String> stringCustomStack = new CustomStack<String>(12, 10);
        for (int i = 0; i < 200; i++) {
            stringCustomStack.push("lyx" + i);
        }
        System.out.println(stringCustomStack.peek());
        System.out.println(stringCustomStack.pop());
        System.out.println(stringCustomStack.elementCount);
    }
}

========END========

转载于:https://my.oschina.net/xinxingegeya/blog/313449

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值