队列的实现(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 }

 

转载于:https://www.cnblogs.com/Huayra/p/10689246.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值