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() + " ");
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有