zoukankan      html  css  js  c++  java
  • JAVA 队列和栈举例

    </pre><pre code_snippet_id="1976124" snippet_file_name="blog_20161109_2_6137834" name="code" class="java"><pre name="code" class="java">import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Stack;
    
    /**
     * 测试jdk中的栈和队列
     * @author scott
     *
     */
    public class TestQueueAndStack {
        
        /**
         * 测试队列
         * <pre>
         * 队列特点,先进先出,后进后出,火车过山洞例子
         * </pre>
         */
        static void testQueue(){
            Queue<String> queue=new LinkedList<String>();
            //添加几个元素
            queue.offer("a");
            queue.offer("b");
            queue.offer("c");
            queue.offer("d");
            queue.offer("e");
            queue.add("1");
            queue.add("2");
            queue.add("3");
            queue.add("4");
            queue.add("5");
            System.out.println("队列中的元素是:"+queue);
            //弹出元素
            queue.poll();
            System.out.println("队列中的元素是:"+queue);
            //查看队列中首个元素,并不移除
            String peek=queue.peek();
            System.out.println("查看队列中首个元素,并不移除:"+peek);
            System.out.println("队列中的元素是:"+queue);
        }
        
        
        /**
         * 测试栈
         * <pre>
         * 先进后出,后进先出,水桶倒水
         * </pre>
         */
        static void testStack(){
            Stack<String> stack=new Stack<String>();
            //添加几个元素
            stack.push("a");
            stack.push("b");
            stack.push("c");
            stack.push("d");
            stack.push("e");
            stack.add("1");
            stack.add("2");
            stack.add("3");
            stack.add("4");
            stack.add("5");
            System.out.println("栈中的元素是:"+stack);
            //弹出元素
            stack.pop();
            System.out.println("栈中的元素是:"+stack);
            //查看栈中首个元素,并不移除
            String peek=stack.peek();
            System.out.println("查看栈中首个元素,并不移除:"+peek);
            System.out.println("栈中的元素是:"+stack);
        }
        
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            testQueue();
            System.out.println("-------栈--------");
            testStack();
        }
    
    }
    
    队列中的元素是:[a, b, c, d, e, 1, 2, 3, 4, 5]
    队列中的元素是:[b, c, d, e, 1, 2, 3, 4, 5]
    查看队列中首个元素,并不移除:b
    队列中的元素是:[b, c, d, e, 1, 2, 3, 4, 5]
    -------栈--------
    栈中的元素是:[a, b, c, d, e, 1, 2, 3, 4, 5]
    栈中的元素是:[a, b, c, d, e, 1, 2, 3, 4]
    查看栈中首个元素,并不移除:4
    栈中的元素是:[a, b, c, d, e, 1, 2, 3, 4]


    
                
    
  • 相关阅读:
    Web 应用程序中的安全向量 – ASP.NET MVC 4 系列
    成员资格、授权 – ASP.NET MVC 4 系列
    数据注解和验证 – ASP.NET MVC 4 系列
    表单和 HTML 辅助方法– ASP.NET MVC 4 系列
    模型(Model)– ASP.NET MVC 4 系列
    Razor 视图引擎 – ASP.NET MVC 4 系列
    视图(View) – ASP.NET MVC 4 系列
    控制器(Controller) – ASP.NET MVC 4 系列
    简介 – ASP.NET MVC 4 系列
    一般处理程序处理图片(动态给图片加上水印、保存缩略图、验证码)
  • 原文地址:https://www.cnblogs.com/zhangxianlong/p/10672528.html
Copyright © 2011-2022 走看看