队列的简单学习

简介:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
      * 1.在Java5中新增加了java.util.Queue接口,用以支持队列的常见操作。该接口扩展了java.util.Collection接口。
     
      * Queue使用时要尽量避免Collection的add()和remove()方法,而是要使用offer()来加入元素,使用poll()来获取
      * 或移除的元素。他们的优点是通过返回值可以判断成功与否,add()和remove()方法在失败的时候会抛出异常。
      * 如果要使用前端而不移除该元素,使用element()或者peek()方法。
     
      * 值得注意的是LinkedList类实现了Queue接口,因此我们可以把LinkedList当初Queue来用。
     
      */
     Queue<String> queue =  new  LinkedList<String>();
     queue.offer( "1" );
     queue.offer( "2" );
     queue.offer( "3" );
     System.out.println( "队列的大小:"  + queue.size());
     System.out.println( "toString : "  + queue.toString());
     
     //循环队列
     for (String str : queue) {
         System.out.println( "队列内容:"  + str);
     }
 
     
     boolean  offerResult = queue.offer( "4" );
     System.out.println( "插入队列的结果:"  + offerResult +  " ,队列的大小:"  + queue.size());
     System.out.println( "toString : "  + queue.toString());
     
     String e = queue.poll();
     System.out.println( "返回删除的头元素 : "  + e);
     System.out.println( "toString : "  + queue.toString());
     
     
     e = queue.peek();
     System.out.println( "返回头元素,不删除头元素 : "  + e);
     System.out.println( "toString : "  + queue.toString());
     
     e = queue.element();
     System.out.println( "返回头元素,不删除头元素 : "  + e);
     System.out.println( "toString : "  + queue.toString());
     
     System.out.println( "是否为空队列  : "  + queue.isEmpty());
     System.out.println( "队列的大小 : "  + queue.size());
     
     System.out.println( "是否包含相关的元素:"  + queue.contains( "1" ));
     System.out.println( "是否包含相关的元素:"  + queue.contains( "2" ));
     
     queue.clear();
     System.out.println( "是否为空队列  : "  + queue.isEmpty());
     System.out.println( "队列的大小 : "  + queue.size());
     System.out.println( "toString : "  + queue.toString());


2.2014-08-10-如果用2两个栈实现一个队列

参考:http://www.cnblogs.com/wanghui9072229/archive/2011/11/22/2259391.html 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package  com.hanchao.test0810;
 
import  java.util.Stack;
 
/***********************
  * 2个栈实现一个队列方法1
  * @author:han    
  * @version:1.0        
  * @created:2014-8-10    
  ***********************
  */
public  class  MyQueue1 {
     /**
      * 思路: 假设有2个栈s1,s2
      * ① 入队时,将元素压入s1
      * ② 出队时,将s1的元素逐个“倒入”(弹出并压入)s2,
      *      将s2的顶元素弹出作为出队元素,
      *   之后再将s2剩下的元素逐个“倒回”s1。
      */
     
     private  Stack<String> s1 =  new  Stack<String>();
     private  Stack<String> s2 =  new  Stack<String>();
     
     /**
      * 入队操作
      * *******************
      * @param str
      * @return
      * *******************
      * @author:wind
      * 2014-8-10 上午11:26:16
      * *******************
      */
     public  boolean  offer(String str) {
         if (str !=  null ) {
             s1.push(str);
             return  true ;
         else  {
             return  false ;
         }
     }
     
     /**
      * 出队操作
      * *******************
      * @return
      * *******************
      * @author:wind
      * 2014-8-10 上午11:27:50
      * *******************
      */
     public  String poll() {
         while (!s1.empty()) {
             //s1不为空,把s1的元素倒入s2
             s2.push(s1.pop());
         }
         //取出s2栈顶的元素
         String str = s2.pop();
         //把s2的元素“倒回”s1
         while (!s2.empty()) {
             s1.push(s2.pop());
         }
         return  str;
     }
     
     /**
      * 如果s1,s2同时为空,没有意义
      * *******************
      * @return
      * *******************
      * @author:wind
      * 2014-8-10 上午11:31:24
      * *******************
      */
     public  boolean  empty() {
         if (s1.empty() && s2.empty()) {
             return  true ;
         }
         return  false ;
     }
     
     public  static  void  main(String[] args) {
         MyQueue1 queue =  new  MyQueue1();
         queue.offer( "aa" );
         queue.offer( "bb" );
         queue.offer( "cc" );
         queue.offer( "dd" );
         
         while (!queue.empty()) {
             System.out.println(queue.poll());
         }
         
     }
     
}

方法2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package  com.hanchao.test0809;
 
import  java.util.Stack;
 
/***********************
  * 用两个栈实现一个队列2
  * @author:han    
  * @version:1.0        
  * @created:2014-8-9    
  ***********************
  */
public  class  MyQueue {
     /**
      * 思路::
      * ①入队时,要判断s2是否为空,为空表示没有出队元素,元素都在s1,可以直接入队
      * 如果s2不为空,我们要把s2中的元素"倒回"s1,再压入入队元素。  
      * ②出队时,要判断s2是否为空,不为空直接弹出s2的顶元素并出队。
      *  如果s2为空,判断s1是否为空,不为空把s1“倒入”s2,再把s2的顶元素取出即可。
      *       
      */
     
     private  Stack<String> s1 =  new  Stack<String>();
     private  Stack<String> s2 =  new  Stack<String>();
     
     /**
      * 入队操作
      * *******************
      * @param str
      * @return
      * *******************
      * @author:wind
      * 2014-8-10 上午10:04:23
      * *******************
      */
     public  boolean  offer(String str) {
         if (s2.isEmpty()) {
             //如果s2为空,则可以直接向s1添加元素
             s1.push(str);
         else  {
             //如果s2不为空,我们要将s2的元素重新倒入s1中
             while (!s2.isEmpty()) {
                 s1.push(s2.pop());
             }
             //再把新元素加入s1中
             s1.push(str);
         }
         return  true ;
     }
     
     /**
      * 出队操作
      * *******************
      * @return
      * *******************
      * @author:wind
      * 2014-8-10 上午10:11:29
      * *******************
      */
     public  String poll() {
         if (!s2.empty()) {
             //如果s2不为空,可以直接出队
             return  s2.pop();
         else  {
             //如果s2为空,要判断s1是否为空,s1不为空时,要把s1的元素“倒入”s2中
             /*while(!s1.empty()) {
                 s2.push(s1.pop());
             }
             return s2.pop();*/
             
             /**
              * 此处优化一下:在出队时,将s1的元素“倒入”s2时,
              * 原在s1栈底的元素,不用“倒入”s2(即只“倒”s1.count() - 1)
              * 可以直接弹出作为出队元素返回。这样可以减少一次压栈的操作
              */
             int size = s1.size();
             if(size > 0) {
                 for(int i = 0; i < size - 1; i++) {
                     s2.push(s1.pop());
                 }
             }
             return s1.pop();
         }
     }
 
     /**
      * 如果s1,s2都为空,没有必要出队和入队
      * *******************
      * @return
      * *******************
      * @author:wind
      * 2014-8-10 上午10:20:11
      * *******************
      */
     public  boolean  empty() {
         if (s1.empty() && s2.empty()) {
             return  true ;
         }
         return  false ;
     }
     
     public  static  void  main(String[] args) {
         MyQueue queue =  new  MyQueue();
         queue.offer( "aa" );
         queue.offer( "bb" );
         queue.offer( "cc" );
         
         while (!queue.empty()) {
             System.out.println(queue.poll());
         }
     }
     
}


方法3:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package  com.hanchao.test0810;
 
import  java.util.Stack;
 
/***********************
  * 两个栈实现一个队列:new
  * @author:han    
  * @version:1.0        
  * @created:2014-8-10    
  ***********************
  */
public  class  MyQueueNew {
     
     /**
      * 思路:s1是入栈的,s2是出栈的。
      *        入队列时:直接压入s1即可。
      *        出队列时:如果s2不为空,把s2中的栈顶元素直接弹出;
      *        否则,把s1的所有元素全部弹出压入s2中,再把弹出的s2的栈顶元素
      */
 
     private  Stack<String> s1 =  new  Stack<String>();
     private  Stack<String> s2 =  new  Stack<String>();
     
     /**
      * 入队操作
      * *******************
      * @param str
      * @return
      * *******************
      * @author:wind
      * 2014-8-10 上午11:15:12
      * *******************
      */
     public  boolean  offer(String str) {
         s1.push(str);
         return  true ;
     }
     
     /**
      * 出队操作
      * *******************
      * @return
      * *******************
      * @author:wind
      * 2014-8-10 上午11:16:18
      * *******************
      */
     public  String poll() {
         /*if(!s2.empty()) {
             //如果s2不为空,直接出队
             return s2.pop();
         } else {
             //如果s2为空,要把s1的元素全部倒入s2中
             while(!s1.empty()) {
                 s2.push(s1.pop());
             }
             return s2.pop();
         }*/
         
         /**
          * 优化一下:
          */
         if(s2.empty()) {
             while(!s1.empty()) {
                 s2.push(s1.pop());
             }
         }
         return s2.pop();
     }
     
     /**
      * 如果s1和s2都为空,没有出队的意义
      * *******************
      * @return
      * *******************
      * @author:wind
      * 2014-8-10 上午11:19:51
      * *******************
      */
     public  boolean  empty() {
         if (s1.empty() && s2.empty()) {
             return  true ;
         }
         return  false ;
     }
     
     public  static  void  main(String[] args) {
         MyQueueNew queue =  new  MyQueueNew();
         queue.offer( "aa" );
         queue.offer( "bb" );
         queue.offer( "cc" );
         queue.offer( "cc1" );
         
         while (!queue.empty()) {
             System.out.println(queue.poll());
         }
         
     }
}


二:用2个队列实现一个栈

参考:http://www.cnblogs.com/kaituorensheng/archive/2013/03/02/2939690.html 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package  com.hanchao.test0809;
 
import  java.util.LinkedList;
import  java.util.Queue;
 
/***********************
  * 用2个队列实现一个栈1
  * @author:han    
  * @version:1.0        
  * @created:2014-8-10    
  ***********************
  */
public  class  MyStack1 {
     
     /**
      * 思路:队列q1,队列q2
      *        q1是专职进出栈的,q2只是一个临时中转站
      *  
      *  入栈时:直接入队列q1即可
      *  出栈时:把q1的除最后一个元素外全部其他元素转移到队列q2中,
      *       然后把刚才剩下q1中的那个元素出队列。
      *       
      *       之后把q2的全部元素转移回q1中。
      */
 
     private  Queue<String> q1 =  new  LinkedList<String>();
     private  Queue<String> q2 =  new  LinkedList<String>();
     
     /**
      * 入栈操作
      * *******************
      * @param str
      * @return
      * *******************
      * @author:wind
      * 2014-8-10 下午12:08:49
      * *******************
      */
     public  String push(String str) {
         q1.offer(str);
         return  str;
     }
     
     /**
      * 出栈操作
      * *******************
      * @return
      * *******************
      * @author:wind
      * 2014-8-10 下午12:09:53
      * *******************
      */
     public  String pop() {
         if (!q1.isEmpty()) {
             //如果q1不为空,把q1的前n-1个元素倒入q2
             while (q1.size() >  1 ) {
                 q2.offer(q1.poll());
             }
             //取得q1的最后一个元素
             String str = q1.poll();
             //把q2的元素重新倒入q1
             while (q2.size() >  0 ) {
                 q1.offer(q2.poll());
             }
             return  str;
         }
         return  null ;
     }
     
     /**
      * 如果q1和q2都为空的话,没有意义
      * *******************
      * @return
      * *******************
      * @author:wind
      * 2014-8-10 下午12:14:29
      * *******************
      */
     public  boolean  empty() {
         if (q1.isEmpty() && q2.isEmpty()) {
             return  true ;
         }
         return  false ;
     }
     
     public  static  void  main(String[] args) {
         MyStack1 stack =  new  MyStack1();
         stack.push( "aa" );  //栈底
         stack.push( "bb" );
         stack.push( "cc" );  //栈顶
         
         while (!stack.empty()) {
             System.out.println(stack.pop());
         }
     }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package  com.hanchao.test0809;
 
import  java.util.LinkedList;
import  java.util.Queue;
 
/***********************
  * 2个队列实现一个栈:2
  * @author:han    
  * @version:1.0        
  * @created:2014-8-10    
  ***********************
  */
public  class  MyStack2 {
     
     /**
      * 思路:队列q1,q2都可以作为进出栈的队列
     
      * 对于两个队列实现一个栈,如果我要把刚放的东西再取出来,
      * 对于队列来说,只能是把之前的东西都先出队,
      * 才能把刚才的放的东西出队。
     
      * 故:那就把之前的那些数据先出队列到队列2中。
      *       两个队列来回交换数据即可。
      */
     
     private  Queue<String> q1 =  new  LinkedList<String>();
     private  Queue<String> q2 =  new  LinkedList<String>();
     
     /**
      * 入栈操作
      * *******************
      * @param str
      * @return
      * *******************
      * @author:wind
      * 2014-8-10 下午12:38:33
      * *******************
      */
     public  String push(String str) {
         if (q2.isEmpty()) {
             //如果q2为空,把元素压入q1中
             q1.offer(str);
         else  if (q1.isEmpty()) {
             //如果q1为空,把元素压入q2中
             q2.offer(str);
         }
         return  str;
     }
 
     /**
      * 出栈操作
      * *******************
      * @return
      * *******************
      * @author:wind
      * 2014-8-10 下午12:39:05
      * *******************
      */
     public  String pop() {
         if (!q1.isEmpty()) {
             //如果q1不为空,从q1出最后一个元素,并把其他的元素移入q2中
             while (q1.size() >  1 ) {
                 q2.offer(q1.poll());
             }
             return  q1.poll();
         else  if (!q2.isEmpty()) {
             //如果q2不为空,从q2出最后一个元素,并把其他的元素移入q1中
             while (q2.size() >  1 ) {
                 q1.offer(q2.poll());
             }
             return  q2.poll();
         }
         return  null ;
     }
     
     /**
      * 如果队列q1,q2都为空,没有必要操作
      * *******************
      * @return
      * *******************
      * @author:wind
      * 2014-8-10 下午12:45:37
      * *******************
      */
     public  boolean  empty() {
         if (q1.isEmpty() && q2.isEmpty()) {
             return  true ;
         }
         return  false ;
     }
     
     public  static  void  main(String[] args) {
         MyStack2 stack =  new  MyStack2();
         stack.push( "aa" );
         stack.push( "bb" );
         stack.push( "cc" );
         stack.push( "dd" );
         
         while (!stack.empty()) {
             System.out.println(stack.pop());
         }
     }
}







     本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/1426863 ,如需转载请自行联系原作者




相关文章
|
4天前
|
人工智能 运维 安全
|
2天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
9天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
814 109
|
3天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
410 9
|
3天前
|
人工智能 测试技术 API
智能体(AI Agent)搭建全攻略:从概念到实践的终极指南
在人工智能浪潮中,智能体(AI Agent)正成为变革性技术。它们具备自主决策、环境感知、任务执行等能力,广泛应用于日常任务与商业流程。本文详解智能体概念、架构及七步搭建指南,助你打造专属智能体,迎接智能自动化新时代。
|
4天前
|
机器学习/深度学习 传感器 算法
Edge Impulse:面向微型机器学习的MLOps平台——论文解读
Edge Impulse 是一个面向微型机器学习(TinyML)的云端MLOps平台,致力于解决嵌入式与边缘设备上机器学习开发的碎片化与异构性难题。它提供端到端工具链,涵盖数据采集、信号处理、模型训练、优化压缩及部署全流程,支持资源受限设备的高效AI实现。平台集成AutoML、量化压缩与跨硬件编译技术,显著提升开发效率与模型性能,广泛应用于物联网、可穿戴设备与边缘智能场景。
185 127