1 package com.liu.queue; 2 3 public class QueueApp { 4 public static void main(String args[]) 5 { 6 Queue theQueue = new Queue(5); 7 8 theQueue.insert(10); 9 theQueue.insert(20); 10 theQueue.insert(30); 11 //theQueue.insert(40); 12 13 theQueue.remove(); 14 theQueue.remove(); 15 theQueue.remove(); 16 17 theQueue.insert(50); 18 theQueue.insert(60); 19 theQueue.insert(70); 20 theQueue.insert(80); 21 22 while(!theQueue.isEmpty()) 23 { 24 long n = theQueue.remove(); 25 System.out.print(n); 26 System.out.print(" "); 27 } 28 System.out.println(""); 29 } 30 } 31 32 class Queue 33 { 34 private int maxsize; 35 private long[] queArray; 36 private int front; 37 private int rear; 38 private int nItems; 39 40 public Queue(int s) 41 { 42 maxsize = s; 43 queArray = new long[maxsize]; 44 front = 0; 45 rear = -1; 46 nItems = 0; 47 } 48 49 public void insert(long j) 50 { 51 if(rear == maxsize-1) 52 rear = -1;//如果尾指针已经到了最后,则循环 53 queArray[++rear] = j; 54 nItems++; 55 } 56 57 public long remove() 58 { 59 long temp = queArray[front++]; 60 if(front == maxsize) 61 front = 0; 62 nItems--; 63 return temp; 64 } 65 66 public long peekFront() 67 { 68 return queArray[front]; 69 } 70 71 public boolean isEmpty() 72 { 73 return (nItems == 0); 74 } 75 76 public boolean isFull() 77 { 78 return (nItems == maxsize); 79 } 80 81 public int size() 82 { 83 return nItems; 84 } 85 86 87 }