编程离不开生活
2016-01-12 09:39
239 查看
[栈和队列] 编程离不开生活,栈就像袋子,先装的东西在以下,后面装的在上面,当然倒出时,也是上面的先出,以下的后面出,这就是所谓的“先进后出,后进先出”栈的原理。而队列就像过安检一样,先安检的先通过。后安检的后通过,这就是队列的思想——“先进先出。后进后出”。
样例: 反转一个栈
样例: 反转一个栈
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()); } }
相关文章推荐
- Python实现对嵌套列表字典按某一元素合并及lambda的使用
- Java多线程用法解析
- C++文件读写操作(将字幕写入txt文件文本)
- 利用default.xml回退代码
- 关于Myeclipse导出war包出现的问题
- WindowManager.LayoutParams全解
- 代码要为现实服务
- Initialization in《The Java® Virtual Machine Specification Java SE 7 Edition》
- Myeclipse中项目重新命名
- Python基础01 Hello World! >>> 结束符 exit()
- 【MATLAB工程人员的福音】MATLAB R2015b的Coder工具使用实例(二)
- 【转载】 C++中this指针的用法详解
- PHP原生--$_FILES[]['type']的类型
- Spring Boot 日志记录 SLF4J
- Spring Boot 启动加载数据 CommandLineRunner
- Spring Boot 静态资源处理
- Spring配置DataSource数据源
- Spring Boot 拦截器
- Spring Boot 过滤器、监听器
- Spring Boot Servlet