zoukankan      html  css  js  c++  java
  • 编程离不开生活

    [栈和队列] 编程离不开生活,栈就像袋子,先装的东西在以下,后面装的在上面,当然倒出时,也是上面的先出,以下的后面出,这就是所谓的“先进后出,后进先出”栈的原理。而队列就像过安检一样,先安检的先通过。后安检的后通过,这就是队列的思想——“先进先出。后进后出”。

     

    样例: 反转一个栈

    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Stack;
    
    public class QuickTestMain {
    
    	/**
    	 * @param args
    	 * @author: --LJH--2014-8-7下午4:23:31
    	 * @return: void
    	 */
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Stack items = new Stack();
    		items.push("he"); // he is at the bottom of the stack
    		items.push("saw");
    		items.push("a");
    		items.push("racecar");
    		reverseStack(items); // now he is at the top
    
    		// print in order pushed:
    		while (items.size() > 0)
    			System.out.println(items.pop());
    	}
    
    	public static void reverseStack(Stack stack) {
    		Queue rev = new LinkedList();
    		while (stack.size() > 0)
    			rev.offer(stack.pop());				
    		while (rev.size() > 0)
    			stack.push(rev.poll());
    	}
    }
    

  • 相关阅读:
    多色图标字体
    css编写规则BEM
    css处理工具PostCss
    vue2.0点击其他任何地方隐藏dom
    vue2.0多页面开发
    Dijkstra算法(邻接矩阵存储)
    kmp算法c++代码实现
    最小生成树(prim算法,Kruskal算法)c++实现
    字符串匹配的KMP算法(转)
    筛选法求素数
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5123330.html
Copyright © 2011-2022 走看看