队列(顺序存储)代码

 
 
  1. publicclassArrayQueue<T>{
  2.  
  3.     private T[]queue;
  4.     privatestaticint size =20;         //队列容量
  5.     privateint front, rear;        //队首、队尾下标
  6.  
  7.     publicArrayQueue(){
  8.         queue=(T[])newObject[size];
  9.         front =0;
  10.         rear =0;
  11.     }
  12.  
  13.     publicvoid add(T t) throws Exception
  14.     {
  15.         if((rear +1)% size == front)
  16.             thrownewException("over flow!");
  17.  
  18.         rear =(rear +1)% size;
  19.         queue[rear]= t;
  20.     }
  21.  
  22.     public T poll() throws Exception
  23.     {
  24.         if(front == rear)
  25.             thrownewException("under flow!");
  26.  
  27.         front =(front +1)% size;
  28.         returnqueue[front];
  29.     }
  30.  
  31.     public boolean isEmpty(){
  32.         return front == rear;
  33.     }
  34.  
  35. }
 
  1. publicclassTest{
  2.  
  3.     publicstaticvoid main(String[] args) throws Exception{
  4.         ArrayQueue<String> q =newArrayQueue<String>();
  5.         q.add("a");
  6.         q.add("b");
  7.         q.add("c");
  8.         q.add("d");
  9.         q.add("e");
  10.         q.add("f");
  11.         q.add("g");
  12.         q.add("h");
  13.         q.add("i");
  14.         q.add("j");
  15.         q.add("k");
  16.         q.add("l");
  17.         q.add("m");
  18.         while(!q.isEmpty()){
  19.             String temp = q.poll();
  20.             System.out.print(temp);
  21.         }
  22.     }
  23.  
  24. }
 
输出
  1. abcdefghijklm
 
 
 
 
 





转载于:https://www.cnblogs.com/Doing-what-I-love/p/5445073.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值