ITeye博客

数据结构学习笔记-栈(JAVA)

f059074251 · 2015-04-07 10:52 · Java · 移动开发 · Markdown

栈是限制在表的一端,进行插入和删除操作的线性表,栈是后进先出 栈的操作: 构造栈 销毁栈 清空栈 计算栈长度 取栈顶元素 元素压栈 元素弹栈 代码: package com.alg.stack; import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; // 栈是限制在表的一端,进行插入和删除操作的线性表,栈是后进先出 //栈的操作:构造栈 销毁栈 清空栈 计算栈长度 取栈顶元素 元素压栈 元素弹栈 // Iterable实现这个可以使用for(T t:stack)循环 public class Stack implements Iterable { // 统计数组内保存的元素数量 private int elementCount; // 保存数据的数组 protected Object[] elementData = null; // 数组的每次增长幅度 protected int capacityIncrement; // 默认数组大小 private static final int DEFAULT_SIZE = 10; // 操作数 private int modCount; public Stack() { this(DEFAULT_SIZE, 0); } public Stack(int capacity) { this(capacity, 0); } public Stack(int capacity, int capacityIncrment) { elementData = newElementArray(capacity); elementCount = 0; this.capacityIncrement = capacityIncrment; } // 取栈顶元素 @SuppressWarnings("unchecked") public T peek() { return (T) elementData[elementCount - 1]; } // 出栈 @SuppressWarnings("unchecked") public T pop() { final int index = --elementCount; T t = (T) elementData[index]; elementData[index] = null; modCount++; return t; } // 入栈 public void push(T object) { // 栈满,增加栈容量 if (elementCount == elementData.length) { growByOne(); } elementData[elementCount++] = object; modCount++; } // 清空栈 public void clear() { for (int i = 0; i < elementCount; i++) { elementData = null; } modCount++; elementCount = 0; } @SuppressWarnings( {"unused", "unchecked"}) private T[] newElementArray(int size) { return (T[]) new Object[size]; } // 如果从节省空间角度考虑capacityIncrement最好设置 private void growByOne() { int adding = 0; // 没有设置增量的情况 if (capacityIncrement <= 0) { // 如果elementData长度为0,就加1,否则就增加elementData.length的一倍 if ((adding = elementData.length) == 0) { adding = 1; } } else { adding = capacityIncrement; } T[] newData = newElementArray(elementData.length + adding); System.arraycopy(elementData, 0, newData, 0, elementCount); elementData = newData; } public int size() { return elementCount; } @SuppressWarnings("unchecked") public synchronized T remove(int location) { if (location < elementCount) { T result = (T) elementData[location]; elementCount--; int size = elementCount - location; // 把location + 1到最后整个拷贝到从location开始到最后 if (size > 0) { System.arraycopy(elementData, location + 1, elementData, location, size); } // 因为中间的某个删除的位置被覆盖,所以需要把最后一位直空 elementData[elementCount] = null; modCount++; return result; } throw new ArrayIndexOutOfBoundsException(location); } @SuppressWarnings("unchecked") public synchronized T elementAt(int location) { if (location < elementCount) { return (T) elementData[location]; } throw new ArrayIndexOutOfBoundsException(location); } // 有这个可以对java新特性for循环的支持 public Iterator iterator() { return new SimpleIterator(); } // 简单了实现迭代器 private class SimpleIterator implements Iterator { int pos = -1; int expectedModCount; int lastPosition = -1; SimpleIterator() { super(); expectedModCount = modCount; } public boolean hasNext() { return pos + 1 < size(); } public T next() { if (expectedModCount == modCount) { try { T result = elementAt(pos + 1); lastPosition = ++pos; return result; } catch (IndexOutOfBoundsException e) { throw new NoSuchElementException(); } } throw new ConcurrentModificationException(); } public void remove() { if (this.lastPosition == -1) { throw new IllegalStateException(); } if (expectedModCount != modCount) { throw new ConcurrentModificationException(); } try { Stack.this.remove(lastPosition); } catch (IndexOutOfBoundsException e) { throw new ConcurrentModificationException(); } expectedModCount = modCount; if (pos == lastPosition) { pos--; } lastPosition = -1; } } }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。