现在的位置: 首页 > 综合 > 正文

算法—–队列

2013年12月05日 ⁄ 综合 ⁄ 共 774字 ⁄ 字号 评论关闭
  1. package com.eshore.sweetop.dataframe;
  2. public class Quene {
  3.     private int head=-1;
  4.     private int tail=0;
  5.     private int[] q;
  6.     
  7.     public Quene(){
  8.         q=new int[100];
  9.     }
  10.     
  11.     public Quene(int n){
  12.         q=new int[n];
  13.     }
  14.     
  15.     public void en(int x){
  16.         if(head==-1){
  17.             head=0;
  18.         }
  19.         q[tail]=x;
  20.         if(tail==q.length-1){
  21.             tail=1;
  22.         }else{
  23.             tail=tail+1;
  24.         }
  25.     }
  26.     
  27.     public int de(){
  28.         int x=q[head];
  29.         if(head==q.length-1){
  30.             head=0;
  31.         }else{
  32.             head=head+1;
  33.         }
  34.         return x;
  35.     }
  36.     
  37.     public static void main(String[] args) {
  38.         Quene q=new Quene();
  39.         q.en(4);
  40.         q.en(3);
  41.         System.out.println(q.de());
  42.         System.out.println(q.de());
  43.     }
  44. }

 

抱歉!评论已关闭.