- 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://www.codesnippet.cn/detail/171220138086.html
来源: http://www.codesnippet.cn/detail/171220138086.html