Java代码讲解队列数据结构
2017-11-11 15:02:18         来源：Hubbert01的博客

```package DataStructure;

/**
* Created by Hubbert on 2017/11/11.
*/
public class Queue {

private int [] arr ;
private int front ; //队头指针
private int rear ;  //队尾指针
private int nItems ;//队列中的个数
private int maxSize;//队列长度

//使用构造函数进行初始化
public Queue( int maxSize ){
this.maxSize = maxSize ;
this.arr = new int [this.maxSize];
this.nItems = 0 ;
this.front = 0;
this.rear = -1 ;
}

public boolean isFull(){
return (nItems == maxSize);//判断队列是否已满
}

public boolean isEmpty(){
return (nItems == 0);//判断队列是否为空
}

//插入
public void insert( int number ){
if(!isFull()){
//处理循环队列
if( rear == (maxSize -1)){
rear = -1;
}
arr[++rear] = number ;
nItems++;
}else{
System.out.println("The Queue is full!!");
}
}

//删除
public int remove(){
if(!isEmpty()){
//处理循环队列
if( front == maxSize ){
front = 0;
}
nItems--;
return arr[front++];

} else {
System.err.println ("The Queue is Empty!!");
return -1;
}
}

public static void main(String [] args){
Queue queue = new Queue(5);
queue.insert(22);
queue.insert(33);
queue.insert(44);
queue.insert(55);
queue.insert(66);

System.out.println("-----------先删除队列中前两个数据------------");
System.out.println("Front--->Rear:");
for( int i =0 ; i < 2 ; i++ ){
System.out.print(queue.remove() + " ");
}

System.out.println("");
System.out.println("-----------继续使用队列------------");
System.out.println("Front--->Rear:");
queue.insert(1);
queue.insert(2);
while (!queue.isEmpty()){
System.out.print(queue.remove() + " ");
}
}
}
```