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());
}
}