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命令的更多相关文章

  1. 算法Sedgewick第四版-第1章基础-001递归

    一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...

  2. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

  3. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

  4. 算法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 ...

  5. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版

    package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...

  6. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)

    一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...

  7. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

  8. 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的

    1. package algorithms.stacks13; /******************************************************************* ...

  9. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法

    1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...

  10. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法

    1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...

随机推荐

  1. 1.Linux和Unix区别

    整理来源于网络:http://blog.csdn.net/xiaojianpitt/article/details/6377419 有很多初学Linux的人比较关心Linux和windows的区别,这 ...

  2. CodeForces - 13D :Triangles(向量法:问多少个蓝点三角形内部无红点)

    Little Petya likes to draw. He drew N red and M blue points on the plane in such a way that no three ...

  3. 团队开发中Git冲突解决

    正常来说我们团队协作开发过程中,冲突是常有的事,下面介绍下本人在开发中的解决办法. 冲突的主要原因就是由于我们开发人员在分支的同一位置写入了不一样的代码,然后合并到主干上导致我们冲突. 方法: 当冲突 ...

  4. 皮肤和DLL和图片等项目文件完全整合到exe中

    C#开发的程序原生界面实在是太丑了,自己又没有美化天赋,所以只能使用皮肤控件了,网上找到了IrisSkin2,包含一个.dll文件和若干ssk后缀的皮肤文件,代码其实很简单的.但是后来发现个问题,就是 ...

  5. BZOJ1116:[POI2008]CLO

    浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...

  6. Mingw版QtCreator调用VS编译的C++库的方法

    https://wenku.baidu.com/view/ae3667fe0b1c59eef8c7b4bc.html

  7. install 命令

    install命令的作用是安装或升级软件或备份数据,它的使用权限是所有用户.install命令和cp命令类似,都可以将文件/目录拷贝到指定的地点.但是,install允许你控制目标文件的属性.inst ...

  8. java中如何将OutputStream转换为InputStream

    在不需要文件生成的情况下,直接将输出流转换成输入流.可使用下面的三种方法: 如果你曾经使用java IO编程,你会很快碰到这种情况,某个类在OutputStream上创建数据而你需要将它发送给某个需要 ...

  9. Spring MVC配置详解(2)---bai

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2 ...

  10. 为什么每个浏览器都有Mozilla字样

    你是否好奇标识浏览器身份的User-Agent,为什么每个浏览器都有Mozilla字样?Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 ( ...