队列的实现(Java)

 

 1 class Queue
 2 {
 3     private int front;
 4     private int rear;
 5     private int[] a;
 6     
 7     public Queue(int size)
 8     {
 9         this.front = this.rear = 0;
10         this.a = new int[size];
11     }
12     
13     public boolean isFull()
14     {
15         return (this.rear + 1) % this.a.length == this.front;
16     }
17     
18     public boolean isEmpty()
19     {
20         return this.rear == this.front;
21     }
22     
23     public void EnQueue(int k) throws Exception
24     {
25         if(this.isFull())
26             throw new Exception("Overflow.");
27         else
28         {
29             this.a[this.rear] = k;
30             this.rear = (this.rear + 1) % this.a.length;
31         }
32     }
33     
34     public int DeQueue() throws Exception
35     {
36         if(this.isEmpty())
37             throw new Exception("Underflow.");
38         else
39         {
40             int key = this.a[front];
41             this.front = (this.front + 1) % this.a.length;
42             return key;
43         }
44     }
45 
46     public int getLength()
47     {
48         return (this.rear - this.front + this.a.length) % this.a.length;
49     }
50 }

 

posted @ 2019-04-11 13:58  Huayra  阅读(118)  评论(0)    收藏  举报