队列详解及java实现 (6)

1 package test; 2 3 public class SqQueue<T>{ 4 private T[] datas;//使用数组作为队列的容器 5 private int maxSize;//队列的容量 6 private int front;//头指针 7 private int rear;//尾指针 8 9 //初始化队列 10 public SqQueue(int maxSize){ 11 if(maxSize<1){ 12 maxSize = 1; 13 } 14 this.maxSize = maxSize; 15 this.front = 0; 16 this.rear = 0; 17 this.datas = (T[])new Object[this.maxSize]; 18 } 19 20 //两个状态:队空&队满 21 public boolean isNull(){ 22 if(this.front == this.rear) 23 return true; 24 else 25 return false; 26 } 27 28 public boolean isFull(){ 29 if((rear+1)%this.maxSize==front) 30 return true; 31 else 32 return false; 33 } 34 35 //初始化队列 36 public void initQueue(){ 37 this.front = 0; 38 this.front = 0; 39 } 40 41 //两个操作:进队&出队 42 public boolean push(T data){ 43 if(isFull()) 44 return false;//队满则无法进队 45 else{ 46 datas[rear] = data;//进队 47 rear = (rear+1) % maxSize;//队尾指针+1. 48 return true; 49 } 50 } 51 public T pop(){ 52 if(isNull()) 53 return null;//对空无法出队 54 else{ 55 T popData = datas[front++];//出队 56 front = (front+1) % maxSize;//队头指针+1 57 return popData; 58 } 59 } 60 61 //get() 62 public T[] getDatas() { 63 return datas; 64 } 65 66 public int getMaxSize() { 67 return maxSize; 68 } 69 70 public int getFront() { 71 return front; 72 } 73 74 public int getRear() { 75 return rear; 76 } 77 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zydyyp.html