栈的实现(Java)

 1 class Stack
 2 {
 3     private int top;
 4     private int[] a;
 5     
 6     public Stack(int size)
 7     {
 8         this.top = -1;
 9         this.a = new int[size];
10     }
11     
12     public boolean isFull()
13     {
14         /*if(this.top == this.a.length - 1)
15             return true;
16         else
17             return false;*/
18         return this.top == this.a.length - 1;
19     }
20     
21     public boolean isEmpty()
22     {
23         return this.top == -1;
24     }
25     
26     public void push(int k) throws Exception
27     {
28         if(this.isFull())
29             throw new Exception("Overflow.");
30         else
31             this.a[++top] = k;
32     }
33     
34     public int pop() throws Exception
35     {
36         if(this.isEmpty())
37             throw new Exception("Underflow.");
38         else
39             return this.a[top--];
40     }
41 }

 

posted @ 2019-04-11 13:51  Huayra  阅读(295)  评论(0编辑  收藏  举报