基于java的数据结构学习手记4——队列

来源:互联网 发布:捷速ocr文字识别软件 编辑:程序博客网 时间:2024/05/29 16:30

4.队列

      在计算机科学中,队列是一种数据结构,有点类似栈,只是在队列中第一个插入的数据项也会是最先被移除(First In First Out,先进先出),而在栈中,最后插入的数据项最先移除(Last In First Out,后进先出)。队列的作用就像电影院前的人们站成的排一样:第一个进入队尾的人将最先到达队头买票。最后排队的人最后才能买到票。

     在计算机(或者网络)操作系统里,有各种队列在安静的工作着。打印作业在打印队列中等待打印。当在键盘上敲击时,也有一个存储键入内容的队列。同样,如果使用文字处理程序敲击一个键,而计算机又暂时要做其他的事,敲击的内容不会丢失,在会排在队列中等待,直到文字处理程序有时间来读取它。利用队列保证了键入内容在处理时其顺序不会改变。

    循环队列java代码实现:

Code:
  1. package Queue;  
  2. //queue.java  
  3. //demonstrate queue  
  4.   
  5. public class queue {  
  6.      private int maxSize;  
  7.      private long[]queArray;  
  8.      private int front;  
  9.      private int rear;  
  10.      private int nItems;  
  11. //..............................................................  
  12.   public queue(int s)//constructor  
  13.   {  
  14.       maxSize=s;  
  15.       queArray=new long[maxSize];  
  16.       front=0;  
  17.       rear=-1;  
  18.       nItems=0;  
  19.   }  
  20. //..............................................................  
  21.   public void insert(long j)  
  22.   {  if(nItems!=maxSize)  
  23.   { if(rear==maxSize-1)     //deal with wraparound  
  24.           rear=-1;  
  25.       queArray[++rear]=j;    //increment rear and insert  
  26.       nItems++;//one more item  
  27.   }  
  28.   else System.out.println("The queue is full,please wait");  
  29.   }  
  30. //..............................................................  
  31.  public  long remove()      //take item from front of queue  
  32.  {    
  33.      if(nItems!=0)  
  34.      {  
  35.      long temp=queArray[front++];  //get value and increase front  
  36.      if(front==maxSize)            //deal with wraparound  
  37.          front=0;  
  38.      nItems--;                      //one less item  
  39.      return temp;  
  40.      }  
  41.      else System.out.println("No item left yet");  
  42.      return 0;  
  43.  }  
  44. //..............................................................  
  45.   public long peekFront()  
  46.   {  
  47.     return queArray[front];    
  48.   }  
  49. //..............................................................  
  50.   public int size()  
  51.   {  
  52.       return nItems;  
  53.   }  
  54. //..............................................................    
  55.  public boolean isEmpty()  
  56.  {  
  57.     return (nItems==0);  
  58.        
  59.  }  
  60.     
  61.     
  62.     
  63.    
  64.    
  65.    
  66.    
  67.    
  68.     
  69. }  
Code:
  1. package Queue;  
  2.   
  3. public class QueueApp {  
  4.   
  5.     /** 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         // TODO Auto-generated method stub  
  10.        queue theQueue=new queue(5);  
  11.          
  12.        theQueue.insert(10);  
  13.        theQueue.insert(20);  
  14.        theQueue.insert(30);  
  15.        theQueue.insert(40);  
  16.          
  17.        theQueue.remove();  
  18.        theQueue.remove();  
  19.        theQueue.remove();  
  20.          
  21.        theQueue.insert(50);  
  22.        theQueue.insert(60);  
  23.        theQueue.insert(70);  
  24.        theQueue.insert(80);  
  25.          
  26.        while(!theQueue.isEmpty())  
  27.        {  
  28.            long n=theQueue.remove();  
  29.            System.out.print(n);  
  30.            System.out.print(" ");  
  31.        }  
  32.        System.out.print("  ");  
  33.        }//end main()  
  34.     } // end class QueueApp  

结果:

Code:
  1. 40 50 60 70 80