寫在前面:
上一篇文章中我們聊到了棧——漫畫趣解什麼是棧?
相信很多小夥伴都理解了棧;
那麼這次,同樣採用漫畫形式,給大家聊一聊什麼是隊列;
思維導圖:
什麼是隊列?
隊列是一種受限的線性表;
隊列只允許在一端進行插入操作,另一端進行刪除操作;
隊列的特性?
允許插入的一端叫隊尾,允許刪除的的一端叫隊頭;
即先進先出,後進後出;
隊列的存儲結構:
代碼實現:
文中完整源碼獲取請關注公衆號《程序員的時光》;
後臺回覆——數據結構源碼,可以獲得常見數據結構代碼;
隊列的順序存儲:
方法類:
1//入隊
2 public void Push_SeqQueue(SeqQueue queue, Object data){
3 if(queue==null){
4 return;
5 }
6 if(data==null){
7 return;
8 }
9 //如果隊列容量小於或等於數組容量
10 if(queue.size==Max_Size){
11 return;
12 }
13 //元素入隊
14 queue.data[queue.size]=data;
15 queue.size++;
16 }
17
18 //返回隊頭元素
19 public Object Front_SeqQueue(SeqQueue queue){
20 if(queue==null){
21 return null;
22 }
23 if(queue.size==0){
24 return null;
25 }
26 //隊頭元素下標爲0
27 return queue.data[0];
28 }
29
30//出隊
31 public void Pop_SeqQueue(SeqQueue queue){
32 if(queue==null){
33 return;
34 }
35 if(queue.size==0){
36 return;
37 }
38 //出隊操作需要移動全部元素
39 for (int i = 0; i < queue.size-1; i++) {
40 queue.data[i]=queue.data[i+1];
41 }
42 queue.size--;
43 }
主函數:
1public static void main(String[] args) {
2 SeqQueueDao seqQueueDao=new SeqQueueDao();
3 //初始化隊列
4 SeqQueue queue=seqQueueDao.Init_SeqQueue();
5
6 //入隊
7 seqQueueDao.Push_SeqQueue(queue,"A");
8 seqQueueDao.Push_SeqQueue(queue,"B");
9 seqQueueDao.Push_SeqQueue(queue,"C");
10 seqQueueDao.Push_SeqQueue(queue,"D");
11 seqQueueDao.Push_SeqQueue(queue,"E");
12
13 //出隊
14 while (queue.size>0){
15 //查找隊頭元素
16 Object o=seqQueueDao.Front_SeqQueue(queue);
17 System.out.println(o);
18 //出隊
19 seqQueueDao.Pop_SeqQueue(queue);
20 }
21 }
運行結果:
隊列的鏈式存儲:
方法類:
1//入隊列
2 public void Push_LinkQueue(LinkQueue queue,Object data){
3 if (queue == null){
4 return;
5 }
6 if (data == null){
7 return;
8 }
9 //創建新插入的結點,也就是隊列頂指針後面的第一個元素,因爲只能在隊列頂進行元素插入或刪除
10 LinkQueueNode newNode=new LinkQueueNode(data,null);
11 //進入插入操作
12 newNode.next=queue.head.next;
13 //隊列頂指針的next等於新插入結點
14 queue.head.next=newNode;
15 //隊列容量加1
16 queue.size++;
17 }
18
19 //出隊列
20 public void Pop_LinkQueue(LinkQueue queue){
21 if (queue == null){
22 return;
23 }
24 if (queue.size == 0){
25 return;
26 }
27 //pPrev指頭結點,pCurrent指頭結點後面的第一個元素,直到pCurrent爲空爲止
28 LinkQueueNode pPrev=queue.head;
29 LinkQueueNode pCurrent=pPrev.next;
30 while (pCurrent.next!=null){
31 pPrev=pCurrent;
32 pCurrent=pPrev.next;
33 }
34 pPrev.next=null;
35 //隊列容量減1
36 queue.size--;
37 }
38
39//返回隊頭元素
40 public Object Front_LinkQueue(LinkQueue queue){
41 if (queue == null){
42 return null;
43 }
44 if (queue.size == 0){
45 return null;
46 }
47 //隊頭元素也就是前面插入的元素,採用循環一直找到隊頭元素
48 LinkQueueNode pCurrent=queue.head;
49 while (pCurrent.next!=null){
50 pCurrent=pCurrent.next;
51 }
52 return pCurrent.data;
53 }
主函數:
1public static void main(String[] args) {
2 LinkQueueDao linkQueueDao=new LinkQueueDao();
3 //初始化隊列
4 LinkQueue queue=linkQueueDao.Init_LinkQueue();
5
6 //入隊列
7 linkQueueDao.Push_LinkQueue(queue,"A");
8 linkQueueDao.Push_LinkQueue(queue,"B");
9 linkQueueDao.Push_LinkQueue(queue,"C");
10 linkQueueDao.Push_LinkQueue(queue,"D");
11 linkQueueDao.Push_LinkQueue(queue,"E");
12
13 //輸出隊列元素
14 while(!linkQueueDao.IsEmpty_LinkQueue(queue)){
15 //查找隊頭元素
16 Object o=linkQueueDao.Front_LinkQueue(queue);
17 System.out.println(o);
18 //出隊列
19 linkQueueDao.Pop_LinkQueue(queue);
20 }
21 }
運行結果:
好了,今天就先分享到這裏了,下期給大家帶來樹的講解!
更多采用漫畫趣解編程知識的文章,歡迎關注我的微信公衆號,一起學習進步!
原創實屬不易,求個關注吧~