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. 面试题13:在O(1)时间删除链表结点

    题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点. 链表结点与函数的定义如下: struct ListNode { int val; ListNode* next; }; ...

  2. BEC listen and translation exercise 7

    Maybe I ought to subscribe to the Engineering Quarterly.也许我应该订阅<工程学季刊>. The performance is sai ...

  3. Codeforces Round #258 (Div. 2)C(暴力枚举)

    就枚举四种情况,哪种能行就是yes了.很简单,关键是写法,我写的又丑又长...看了zhanyl的写法顿时心生敬佩.写的干净利落,简直美如画...这是功力的体现! 以下是zhanyl的写法,转载在此以供 ...

  4. Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(一)

    最近用vue2做了一个微信商城项目,因为做的比较仓促,所以一边写一下整个流程,一边稍做优化. 项目github地址:https://github.com/seven9115/vue-fullstack ...

  5. 【LeetCode】075. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  6. java 实现插入排序

    sorted数组第0个位置没有放数据 从sorted数组的第二个数据开始处理 package com.learn; public class InsertionSort { public static ...

  7. 总结:实体类和(XML或二进制)之间相互转(序列化和反序列化)

    XML和实体类之间相互转换(序列化和反序列化) C# XML反序列化与序列化举例:XmlSerializer XML文件与实体类的互相转换   通过我前面的几篇收藏的文章,今天来自己做个对实体类对象序 ...

  8. 关于postman、postman interceptor的安装、配置问题

    由于app中有一些鉴权问题,需要携带浏览器的cookie. 不然的话不能够正确测试接口,就在chrome(这里下载的来源是Google商店)中添加了postman interceptor插件. 然后发 ...

  9. Azure VM复制

    目前Azure上复制VM可以有多种方法: 1 创建User Image,可以快速复制多台VM,但目前托管磁盘只支持Generalized的模式,需要对User和配置进行重置. 2 从VHD或托管磁盘复 ...

  10. asp.net过滤HTML标签,只保留换行与空格

    自己从网上找了一个过滤HTML标签的方法,我也不知道谁的才是原创的,反正很多都一样.我把那方法复制下来,代码如下: /// <summary> /// 去除HTML标记 /// </ ...