- package com.zhb.corejava.util;
- public class Stack<E> {
- private E[] elementData;
- private int top;
-
- private int count;
-
- @SuppressWarnings("unchecked")
- public Stack(int size) {
- this.elementData = (E[]) new Object[size];
- top = -1;
- count = 0;
- }
- public Stack() {
- this(10);
- }
-
-
- public int getSize(){
- return elementData.length;
- }
-
-
- public int getElementCount() {
- return count;
- }
-
-
- public boolean isEmpty(){
- return top == -1;
- }
-
-
- public boolean isFull(){
- return top == (elementData.length-1);
- }
-
-
- public void push(E object){
- if(isFull()){
- throw new RuntimeException("栈已经满");
- }else{
- elementData[++top] = object;
- count++;
- }
- }
-
-
- public E pop(){
- if(isEmpty()){
- throw new RuntimeException("栈是空的");
- }else{
- count--;
- return elementData[top--];
- }
- }
- public E peek(){
- if(isEmpty()){
- throw new RuntimeException("栈是空的");
- }else{
- return elementData[top];
- }
- }
- }
posted @
2015-04-14 22:50
win24
阅读(
107)
评论()
编辑
收藏
举报