class arrayqueue{
private int Maxsize; //队列最大值
private int front; //队首 栈的下面
private int real; //队尾 栈的上面
private int[] arr; //模拟队列
public arrayqueue(int arrmaxsize){ //构造方法
Maxsize= arrmaxsize;
arr =new int[Maxsize];
front =-1; //指向队列头部的前一个位置
real =-1; //指向队尾的数据
}
// 判断队列是否满
public boolean isFull(){
return real == Maxsize-1;
}
//判断是否为空
public boolean isempty(){
return front ==real;
}
// 添加数据到队列
public void addduilie(int n){
if (isFull()){
System.out.println("is full");
return;
}
real++; //让队尾后移
arr[real]=n;
}
// 获取队列的数据
public int getduilie() throws Exception {
// 判断队列是否空
if (isempty()){
throw new Exception("队列空不能取数据");
}
front++;
return arr[front];
}
// 显示队列的所有数据
public void showshulie(){
// 遍历
if (isempty()){
System.out.println("队列空的,没有数据");
return;
}
for (int i=0;i<arr.length;i++){
System.out.printf("arr[%d]=%d\n" , i, arr[i]);
}
}
// 显示队列的头部
public int headqueue(){
if (isempty()){
throw new RuntimeException("队列为空");
}
return arr[front+1];
}
}
数据结构--用数组模拟队列
最新推荐文章于 2024-08-03 21:06:31 发布