1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.fxr.stack;
 
/**
 * Created by airycode on 2017/3/12.
 */
public class MyStack {
    private long [] arr;
    private int top;
 
    /**
     *默认的构造方法
     */
    public MyStack(){
        arr = new long[10];
        top = -1;
    }
    /**
     * 带参数的构造方法
     */
    public MyStack(int maxSize){
        arr = new long[maxSize];
        top = -1;
    }
 
    //添加数据
    public void push(long value){
        arr[++top] = value;
    }
    //删除数据
    public long pop(){
        return arr[top--];
    }
    //查看数据
    public long peek(){
        return arr[top];
    }
    //判断是不是为空
    public boolean isEmpty(){
        return top == -1;
    }
    //判断是不是满
    public boolean isFull(){
        return top == arr.length-1;
    }
 
}
 
 
------------------------------------------------------------------------------------------------
package com.fxr.stack;
 
/**
 * Created by airycode on 2017/3/12.
 */
public class TestMyStack {
 
    public static void main(String[] args){
        MyStack myStack = new MyStack(4);
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        myStack.push(4);
 
        System.out.println(myStack.isEmpty());
        System.out.println(myStack.isFull());
        System.out.println(myStack.peek());
 
    }
 
}

  

posted on   airycode  阅读(175)  评论(0编辑  收藏  举报

努力加载评论中...

导航

点击右上角即可分享
微信分享提示