java 队列 容量有限_java中怎么设置队列容量

public class queue{

private object[] data=null;

private int maxsize; //队列容量

private int front; //队列头,允许删除

private int rear; //队列尾,允许插入

//构造函数

public queue(){

this(10);

}

public queue(int initialsize){

if(initialsize >=0){

this.maxsize = initialsize;

data = new object[initialsize];

front = rear =0;

}else{

throw new runtimeexception("初始化大小不能小于0:" + initialsize);

}

}

//判空

public boolean empty(){

return rear==front?true:false;

}

//插入

public boolean add(e e){

if(rear== maxsize){

throw new runtimeexception("队列已满,无法插入新的元素!");

}else{

data[rear++]=e;

return true;

}

}

//返回队首元素,但不删除

public e peek(){

if(empty()){

throw new runtimeexception("空队列异常!");

}else{

return (e) data[front];

}

}

//出队

public e poll(){

if(empty()){

throw new runtimeexception("空队列异常!");

}else{

e value = (e) data[front]; //保留队列的front端的元素的值

data[front++] = null; //释放队列的front端的元素

return value;

}

}

//队列长度

public int length(){

return rear-front;

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值