• 队列(顺序存储)代码


     
     
    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
     
     
     
     
     





  • 相关阅读:
    区分.net中的virtual new 与override
    DOM
    两个php函数
    中文字符,全角字符的正则表达式
    CSS实现居中代码大全
    xp纯净版
    A Visit to Two National Parks: Mount Rainier and Valley Forge
    8、检测浏览器和操作系统
    客户
    American History: The Reagan Years
  • 原文地址:https://www.cnblogs.com/Doing-what-I-love/p/5445073.html
Copyright © 2020-2023  润新知