数据结构-简单栈的实现

来源:互联网 发布:vnc mac 连windows 编辑:程序博客网 时间:2024/05/20 21:48
简单的栈的实现,栈元素为类型为int,利用一个数组实现,使用时,构造栈有两种方式,一种为不指定栈的大小,默认原始大小为4,当元素超过当前大小时,则栈大小增长一倍,另一种为指定栈的大小,超过指定大小时抛出异常。栈的操作有:
push(int value)  压栈
int pop()  弹栈
int peek()  查看栈顶
boolean isEmpty()  判断栈是否为空
boolean isFull()  判断是否已满,若为自动增长模式,即判断是否已达到当前栈的大小的值
public int getSize()  获取栈的大小

/** * Created by logaxy on 2017/1/3. */public class MyStack {    private boolean actoIncrement = true;//栈是否自增长,当指定大小时为否    private static int INITIALSIZE = 4;//不指定栈大小时的初始化大小    private int array[];//栈以一个数组实现    private int top;    MyStack() {        array = new int[INITIALSIZE];        top = -1;    }    MyStack(int maxSize) {        actoIncrement=false;        if (maxSize < 0 || maxSize == 0)            throw new IllegalArgumentException();        array = new int[maxSize];        top = -1;    }    void push(int value) {        if (isFull()) {            if (actoIncrement) {                int[] temp = array;                array = new int[2 * array.length];                System.arraycopy(temp, 0, array, 0, temp.length);            } else {                throw new IndexOutOfBoundsException();            }        }        array[++top] = value;    }    int pop() {        if (isEmpty())            throw new IndexOutOfBoundsException();        return array[top--];    }    int peek() {        if (isEmpty())            throw new IndexOutOfBoundsException();        return array[top];    }    boolean isEmpty() {        return (top == -1);    }    boolean isFull() {        return !(top < array.length - 1);    }    public int getSize() {        return array.length;    }}


 
测试程序:
public class Main {    public static void main(String[] args) {        testStack();    }    public static void testStack(){        MyStack myStack=new MyStack();        myStack.push(1);        myStack.push(2);        myStack.push(3);        myStack.push(4);        System.out.println("Top Is: "+myStack.peek());        System.out.println("Size IS:"+myStack.getSize());        System.out.println("Is Full: "+myStack.isFull());        myStack.push(5);        myStack.push(6);        System.out.println("Top Is: "+myStack.peek());        System.out.println("Size IS:"+myStack.getSize());        System.out.println("Is Full: "+myStack.isFull());        System.out.println(myStack.pop());        System.out.println(myStack.pop());        System.out.println(myStack.pop());        System.out.println(myStack.pop());        System.out.println(myStack.pop());        System.out.println(myStack.pop());        System.out.println("Is Empty:"+myStack.isEmpty());        System.out.println("Size IS:"+myStack.getSize());    }}

测试结果:
Top Is: 4
Size IS:4
Is Full: true
Top Is: 6
Size IS:8
Is Full: false
6
5
4
3
2
1
Is Empty:true
Size IS:8
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 向客人开错房间怎么办 工伤认定没有证人证言怎么办? 一个月没来月经怎么办14岁 刮了胡子下巴痒怎么办 月经多怎么办喝什么好 大姨妈不调怎么办来了 多人被骗没证据怎么办 一直坏肚子拉水怎么办 犯人在监狱有高危高血压怎么办 丈夫出轨他死都不承认怎么办 没写欠条的债该怎么办 被尾随回家知道住处怎么办 微信记录全没了怎么办 微信记录没有了怎么办 ipad登录不了下不了软件怎么办 苹果手机打开自动时间不对怎么办 魅族手机时间不同步怎么办 小米手环时间不准怎么办 电脑网络dns配置错误怎么办 去泰国手机要打电话怎么办 苹果6s音量小怎么办 三星0n7充电慢怎么办 在国外接受不到验证码怎么办 ip地址错误网络无法连通怎么办 注册微信收不到验证码怎么办 安卓手机收不到短信怎么办 手机被短信验证码轰炸怎么办 美国访学一年手机卡怎么办方便 小狗吃了葡萄皮怎么办 ios迅雷下载不了的资源怎么办 ipad软件商店内容少怎么办? 脸摔伤后留下黑印怎么办 不确定孩子是不是老公的怎么办 孩子接种证丢了怎么办 孩子的出生证丢了怎么办 私秘边上肿了怎么办 书法作品少写一个字怎么办 炉石传说ios闪退怎么办 ck手表表链大了怎么办 天梭手表卡扣坏了怎么办 机械表平时不戴怎么办