数据结构-栈,,package com.
分享于 点击 43803 次 点评:35
数据结构-栈,,package com.
package com.structures.list;public class JStack<E> { public E[] data; public int size; public JStack() { final int INITAL_CAPACITY = 10; data = (E[]) new Object[INITAL_CAPACITY]; size = 0; } public boolean isEmpty() { return size == 0; } public E push(E e) { if (size == data.length) { E[] bigger = (E[]) new Object[size * 2]; for (int i = 0; i < data.length; i++) { bigger[i] = data[i]; } data = bigger; } data[size] = e; size++; return e; } // Delete the top public E pop() { if (size == 0) { throw new IndexOutOfBoundsException("Size :" + size); } E e = data[size-1]; data[size-1] = null; size--; return e; } // Get the top ,but don't delete it public E peek() { if (size == 0) { throw new IndexOutOfBoundsException("Size :" + size); } E e = data[size-1]; return e; }}//该片段来自于http://byrx.net
用户点评