arraystack.java

来自「java版的数据结构的完全代码 免费提供了 学习数据结构的请下载」· Java 代码 · 共 59 行

JAVA
59
字号
// Introduced in Chapter 5/** An array-based Stack. */public class ArrayStack<E> implements Stack<E> {  /** Array of items in this Stack. */  private E[] data;  /** Number of items currently in this Stack. */  private int size;  /** The Stack is initally empty. */  public ArrayStack() {    data = (E[])(new Object[1]); // This causes a compiler warning    size = 0;  }  public boolean isEmpty() {    return size == 0;  }  /** Return true if data is full. */  protected boolean isFull() {    return size == data.length;  }  public E peek() {    if (isEmpty()) {      throw new EmptyStructureException();    }    return data[size - 1];  }  public E pop() {    if (isEmpty()) {      throw new EmptyStructureException();    }    size--;    return data[size];  }  public void push(E target) {    if (isFull()) {      stretch();    }    data[size] = target;    size++;  }  /** Double the length of data. */  protected void stretch() {    E[] newData = (E[])(new Object[data.length * 2]); // Warning    for (int i = 0; i < data.length; i++) {      newData[i] = data[i];    }    data = newData;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?