• 队列(顺序存储)代码


     
     
    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
     
     
     
     
     





  • 相关阅读:
    java-transaction事件
    Cookie,Session基础知识
    JSP基础笔记
    PHP----学生管理系统
    C语言程序设计-----贪吃蛇
    2019年数维杯三场征战赛
    回忆2018年高教杯数学建模大赛
    iPad横屏模式研究
    IOS UIWebView截获html并修改便签内容,宽度自适应
    如何保持iOS上键盘出现时输入框不被覆盖
  • 原文地址:https://www.cnblogs.com/Doing-what-I-love/p/5445073.html
Copyright © 2020-2023  润新知