队列具有FIFO(先进先出)的特点,此处笔者采用数组的方式实现一个简单的队列功能。
1、Java代码实现
(1)、定义相关属性
//数组容量 private int capacity; //当前数组长度 private int length = 0; //具体数据 private Object[] elements; //队首指针(指向队首元素的上一个位置) private int first = -1; //队尾指针(指向队尾元素的位置) private int end = -1;
(2)、实现相关操作方法
1 /** 2 * 构造方法,初始化数组element 3 * @param capacity 4 */ 5 public ArrQueue(int capacity){ 6 if (capacity <=0){ 7 throw new RuntimeException("capacity must Greater than zero!"); 8 } 9 this.capacity = capacity; 10 this.elements = new Object[capacity]; 11 } 12 13 /** 14 * 队列是否已满 15 * @return 16 */ 17 public boolean isFull(){ 18 return this.length == this.capacity; 19 } 20 21 /** 22 * 队列是否为空 23 */ 24 public boolean isEmpty(){ 25 return this.first == this.end; 26 } 27 /** 28 * 元素添加 29 * @param element 30 */ 31 public void addElement(int element){ 32 if (isFull()){ 33 throw new RuntimeException("the queue is full!"); 34 } 35 length++; 36 if (isEmpty()){ 37 this.first = end =-1; 38 } 39 this.elements[++this.end] = element; 40 } 41 42 /** 43 * 元素移除 44 * @return 45 */ 46 public Object remove(){ 47 if (isEmpty()){ 48 throw new RuntimeException("the queue is empty!"); 49 } 50 length--; 51 //移除元素之后,队首指针向后移动一位 52 return elements[++this.first]; 53 } 54 55 /** 56 * 打印所有的元素(没有出队列) 57 */ 58 public void showAllElements(){ 59 for (int i = this.first+1; i <= this.end; i++) { 60 System.out.print(this.elements[i]+" "); 61 } 62 System.out.println(); 63 } 64 65 /** 66 * 返回当前队列长度 67 * @return 68 */ 69 public int getLength(){ 70 return this.length; 71 } 72 73 /** 74 * 清空队列 75 */ 76 public void clearQueue(){ 77 this.end = this.first = -1; 78 this.length = 0; 79 for (int i = 0; i < this.elements.length; i++) { 80 elements[i] = null; 81 } 82 } 83 84 /** 85 * 销毁队列 86 */ 87 public void destoryQueue(){ 88 this.elements = null; 89 this.end = this.first = -1; 90 this.length = 0; 91 }
2、测试
1 public static void main(String[] args) { 2 ArrQueue arrQueue = new ArrQueue(10); 3 for (int i = 0; i < 10; i++) { 4 arrQueue.addElement(i); 5 } 6 System.out.println("==========打印所有的元素================="); 7 arrQueue.showAllElements(); 8 9 System.out.println("===========所有元素出队列================================="); 10 for (int i = 0; i < 10; i++) { 11 System.out.print(arrQueue.remove()+" "); 12 } 13 System.out.println(); 14 System.out.println("==========打印所有的元素================="); 15 arrQueue.showAllElements(); 16 17 System.out.println("==========元素全部移除队列之后,此时队首和队尾指针、队列长度=============="); 18 System.out.println("队首:"+arrQueue.first+",队尾:"+arrQueue.end+",队列长度:"+arrQueue.length); 19 20 System.out.println("=======再次插入一个元素==========================="); 21 arrQueue.addElement(10); 22 23 System.out.println("==========打印所有的元素================="); 24 arrQueue.showAllElements(); 25 26 System.out.println("==========插入一个元素之后,此时队首和队尾指针、队列长度=============="); 27 System.out.println("队首:"+arrQueue.first+",队尾:"+arrQueue.end+",队列长度:"+arrQueue.length); 28 29 System.out.println("===========将这个元素出队列========================================"); 30 System.out.println(arrQueue.remove()); 31 32 System.out.println("==========此时队首和队尾指针、队列长度=============="); 33 System.out.println("队首:"+arrQueue.first+",队尾:"+arrQueue.end+",队列长度:"+arrQueue.length); 34 }
3、结果
1 ==========打印所有的元素================= 2 0 1 2 3 4 5 6 7 8 9 3 ===========所有元素出队列================================= 4 0 1 2 3 4 5 6 7 8 9 5 ==========打印所有的元素================= 6 7 ==========元素全部移除队列之后,此时队首和队尾指针、队列长度============== 8 队首:9,队尾:9,队列长度:0 9 =======再次插入一个元素=========================== 10 ==========打印所有的元素================= 11 10 12 ==========插入一个元素之后,此时队首和队尾指针、队列长度============== 13 队首:-1,队尾:0,队列长度:1 14 ===========将这个元素出队列======================================== 15 10 16 ==========此时队首和队尾指针、队列长度============== 17 队首:0,队尾:0,队列长度:0