實現一個簡單的隊列(底層數組)

隊列的特色數組

  1. 先進先出(FIFO)
  2. 容許在表的一端插入數據,在另外一端刪除數據

 

所需元素測試

  1. 數組queueArray
  2. 指向隊頭的指針front
  3. 指向隊尾的指針rear
  4. 記錄隊列元素數量的elements
  5. 數組的初始化大小maxSize

 

分析實現this

  1. 在實例化隊列類時肯定數組大小並初始化數組
  2. 肯定隊列具備的功能:spa

    (1)EnQueue(Object obj)   插入元素,即元素入隊,而且是在隊尾入隊指針

    (2)DeQueue()   刪除元素,即元素出隊,而且是在隊頭出隊code

    (3)isEmpty()  判空blog

    (4)isFull()   判滿隊列

    (5)getHead()   查看隊頭元素,只查看,不刪除element

    (6)length()  查看隊列長度get

    (7)clear()  清空隊列


 

代碼實現

 1 public class Queue {
 2 
 3     /**
 4      * queueArray 底層數組
 5      * front 隊頭
 6      * rear 隊尾
 7      * elements 隊列元素
 8      * maxSize 數組最大容量
 9      */
10     private Object[] queueArray;
11     private int front;
12     private int rear;
13     private int elements;
14     private int maxSize;
15 
16     /**
17      * 初始化操做
18      * @param maxSize
19      */
20     public Queue(int maxSize) {
21         this.maxSize = maxSize;
22         this.queueArray = new Object[maxSize];
23         this.front = 0;
24         this.rear = 0;
25         this.elements = 0;
26     }
27 
28     /**
29      * 判斷隊列是否爲空
30      * @return
31      */
32     public boolean isEmpty() {
33         return (elements == 0);
34     }
35 
36     /**
37      * 判斷隊列是否爲滿隊列
38      * @return
39      */
40     public boolean isFull() {
41         return (elements == maxSize);
42     }
43 
44     /**
45      * 插入元素,即元素入隊,而且是在隊尾入隊
46      * @param data
47      */
48     public void enQueue(Object data) {
49         if (isFull()) {
50             throw new RuntimeException("隊列已滿");
51         }
52         queueArray[rear++] = data;
53         elements++;
54     }
55 
56     /**
57      * 刪除元素,即元素出隊,而且是在隊頭出隊
58      * @return
59      */
60     public Object deQueue() {
61         if (isEmpty()) {
62             throw new RuntimeException("空隊列");
63         }
64         // 其實這裏並無真正刪除數組裏的元素,只是訪問指針往上移,即不提供訪問被刪除元素的方式
65         Object data = queueArray[front++];
66         elements--;
67         return data;
68     }
69 
70     /**
71      * 查看隊頭元素,只查看,不刪除
72      * @return
73      */
74     public Object getHead() {
75         Object data = queueArray[front];
76         return data;
77     }
78 
79     /**
80      * 查看隊列長度
81      * @return
82      */
83     public int length() {
84         return elements;
85     }
86 
87     /**
88      * 清空隊列
89      */
90     public void clear() {
91         while (front != rear) {
92             deQueue();
93         }
94     }
95 }

 

測試

 1 public class QueueTest {
 2 
 3     public static void main(String[] args) {
 4 
 5         Queue queue = new Queue(4);
 6 
 7         System.out.println("隊列是否爲空? " + queue.isEmpty());
 8 
 9         queue.enQueue(11);
10         queue.enQueue(2);
11         queue.enQueue(5);
12         queue.enQueue(67);
13 
14         System.out.println("隊列是否已滿? " + queue.isFull());
15         System.out.println("隊頭元素: " + queue.getHead());
16         System.out.println("隊列長度: " + queue.length());
17 
18         System.out.print("打印隊列元素: ");
19         while (!queue.isEmpty()) {
20             Object o = queue.deQueue();
21             System.out.print(o + "\t");
22         }
23         System.out.println();
24 
25         /*queue.enQueue(5);
26         queue.enQueue(67);*/
27         // 清空隊列
28         queue.clear();
29         System.out.println("清空隊列後,隊列是否爲空? " + queue.isEmpty());
30     }
31 }

 

結果

隊列是否爲空? true
隊列是否已滿? true
隊頭元素: 11
隊列長度: 4
打印隊列元素: 11    2    5    67    
清空隊列後,隊列是否爲空? true

 


 

總結

  1. 只容許在隊尾rear插入元素;
  2. 只容許在隊頭front刪除元素;
  3. 本例子插入和刪除都是隊頭隊尾指針的移動,所存儲的元素並不會移動;
  4. 刪除的元素其實還存在數組中,只是不提供訪問的方法;
  5. 使用數組實現,擴容困難。
相關文章
相關標籤/搜索