写在前面:
上一篇文章中我们聊到了栈——漫画趣解什么是栈?
相信很多小伙伴都理解了栈;
那么这次,同样采用漫画形式,给大家聊一聊什么是队列;
思维导图:
什么是队列?
队列是一种受限的线性表;
队列只允许在一端进行插入操作,另一端进行删除操作;
队列的特性?
允许插入的一端叫队尾,允许删除的的一端叫队头;
即先进先出,后进后出;
队列的存储结构:
代码实现:
文中完整源码获取请关注公众号《程序员的时光》;
后台回复——数据结构源码,可以获得常见数据结构代码;
队列的顺序存储:
方法类:
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 }
运行结果:
好了,今天就先分享到这里了,下期给大家带来树的讲解!
更多采用漫画趣解编程知识的文章,欢迎关注我的微信公众号,一起学习进步!
原创实属不易,求个关注吧~