算法Sedgewick第四版-第1章基础-025-用队列实现unix下的Directory命令
package algorithms.util; /******************************************************************************
* Compilation: javac Directory.java
* Execution: java Directory directory-name
* Dependencies: Queue.java StdOut.java
*
* Prints out all of the files in the given directory and any
* subdirectories in level-order by using a queue. Also prints
* out their file sizes in bytes.
*
* % java Directory .
*
******************************************************************************/ import java.io.File; import algorithms.ADT.Queue; public class Directory { public static void main(String[] args) {
Queue<File> queue = new Queue<File>();
File root = new File(args[0]); // root directory
if (!root.exists()) {
StdOut.println(args[0] + " does not exist");
return;
} queue.enqueue(root);
while (!queue.isEmpty()) {
File x = queue.dequeue();
if (!x.isDirectory()) {
StdOut.println(x.length() + ":\t" + x);
}
else {
File[] files = x.listFiles();
for (int i = 0; i < files.length; i++)
queue.enqueue(files[i]);
}
}
} }
算法Sedgewick第四版-第1章基础-025-用队列实现unix下的Directory命令的更多相关文章
- 算法Sedgewick第四版-第1章基础-001递归
一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)
一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版
package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)
一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)
一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...
- 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的
1. package algorithms.stacks13; /******************************************************************* ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法
1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法
1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...
随机推荐
- GridView的简单使用
测试代码: activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/a ...
- 分布式_理论_04_ 3PC
一.前言 五.参考资料 1.分布式理论(四)—— 一致性协议之 3PC 2.分布式理论(四) - 3PC协议 3.
- @angular/cli项目构建--httpClient
app.module.ts update imports: [ HttpClientModule] product.component.ts import {Component, OnInit} fr ...
- ps6-图层基础与操作技巧
1.图层的新建.复制与删除 ctrl+j:复制图层,可以用复制选区作为新图层 Shift+Ctrl+Alt+e:在新的空白图层将下面所有的图层合并为一个图层. 2.选择复制与链接图层 在移动图层时,按 ...
- Shell脚本备份Mongodb数据库
目录 环境还原 环境创建 编写shell脚本 准备文件 创建shell脚本 执行shell脚本 进阶版 感谢 诚邀访问我的个人博客:我在马路边 更好的阅读体验点击查看原文:Shell脚本备份Mongo ...
- BerOS file system
The new operating system BerOS has a nice feature. It is possible to use any number of characters '/ ...
- RabbitMQ 权限分离&HA操作文档
概要 默认情况下,使用帐号guest帐号登陆MQ,所有用户的queue信息,全部创建在根目录/的virtual host下,而这样,就会导致,任一用户登录后,都能看到其他用户的queue信息. 针对以 ...
- Phong光照模型的Shader实现
计算反射向量 Phong用到的是反射向量,计算反射向量的公式是 R = 2*N(dot(N, L)) - L 这个公式是根据向量的投影公式以及平行四边形法则推导出来的 详细步骤请看这篇文章,讲的非常好 ...
- QAbstractSocket::connectToHost() called when already looking up or connecting/connected to
tcpSocket_connect_HBJ->abort();//取消已有连接,重置套接字,tcpSocket_connect_HBJ是QTcpSocket类的对象 就不会报错了.
- java中static的学习
1.static引入: 通常来说,当创建一个类是,就是在描述那个类的对象的外观与行为.除非用new创建那个类的对象,否则实际并未获取任何对象.当执行new来创建对象时,数据存储空间才被分配,七方法才供 ...