在这里插入代码片package queue;
/**
* @author moon
* @create 2021-03-02 7:57
*/
//定义一个队列
public class queue {
private int maxsize;
private int front;
private int rear;
private int []arr;
public queue(int maxsize){
//初始化队列
this.maxsize=maxsize;
arr=new int[maxsize];
front=-1;
rear=-1;
}
//判断是否满队列
public boolean isFull(){
return rear==maxsize-1;
}
//判断是否队空
public boolean isnull(){
return rear==front;
}
//入队
public void push(int value){
if(isFull()){
System.out.print("队满");
}
rear++;
arr[rear]=value;
}
//出队
public int getqueue(){
//判断空
if(isnull()){
throw new RuntimeException("空");
}
front++;
return arr[front];
}
//查看队列
public void list(){
for(int i=rear;i>-1;i--){
System.out.print(arr[i]);
}
}
public static void main(String[]args){
queue q=new queue(10);
q.push(2);
q.push(2);
q.push(3);
q.push(5);
q.push(7);
q.push(2);
q.list();
}
}
java队列
最新推荐文章于 2022-05-30 14:07:58 发布