首页 > 编程语言 > 详细

用数组模拟队列(非环形队列)

时间:2019-09-17 10:38:49      阅读:105      评论:0      收藏:0      [点我收藏+]
package com.suredata.MQTT;


public class MyQueue {
//队列最大容量
private int maxSize=3;
//取出数据时减一
private int front=-1;
//添加数据时加一
private int rear=-1;
private Object[] object;
public void myPush(Object obj){
if(++rear<maxSize){
object[rear]=obj;
}else System.out.println("队列已满!");

}
public Object myPull(){
Object obj=null;
if(++front<=rear){
obj=object[front];
System.out.println(obj);
}else{
System.out.println("队列已为空!");
}

return obj;
}
public void myList(){
for(int i=0;i<object.length;i++){
if(object[i]!=null){
System.out.print(object[i]+"\t");
}
}

}
public int size(){
return maxSize;
}
public void initialize(int cap){
if(cap<0){
object=new Object[maxSize];
}else object=new Object[cap];
}
public static void main(String[] args) {
MyQueue myQueue=new MyQueue();
myQueue.initialize(5);
myQueue.myPush("hello");
myQueue.myPush(34);
myQueue.myPush(true);
myQueue.myPull();
myQueue.myPull();
myQueue.myPull();
myQueue.myPull();
System.out.println(myQueue.size());

}

}

用数组模拟队列(非环形队列)

原文:https://www.cnblogs.com/ljywxk/p/11531566.html

(2)
(2)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!