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. Django 基础 ORM系统

    Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...

  2. Java NIO阻塞式通信

    package com.nio.t; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.By ...

  3. KVM-环境安装

    1.操作系统安装 本文采用Centos6.4X64操作系统,也可以采用RHEL/CentOS6.x. (1)查看系统版本.内核版本 ##查看系统版本 [root@KVM ~]# cat /etc/re ...

  4. Ajax与后台的交互

    Ajax Java 交互 jsp代码 <%@ page language="java" import="java.util.*" pageEncoding ...

  5. PHP中不用第三个变量交换两个变量的值

    相信大家在PHP面试或者学习中经常会遇到这个问题就是“不用第三个变量来交换两个变量的值”,今天正对这个问题来讨论一下: 第一种方法:首先会想到的 这种方法简单可行,顺利的交换了两个变量的值. 第二种方 ...

  6. CodeForces 589H Tourist Guide

    传送门 题目大意 给定$n$个点$m$条边的无向图,有$K$个关键点,你要尽可能的让这些关键点两两匹配,使得所有点对之间可以通过简单路径连接且任意两个简单路径没有重复的边(可以是共同经过一个点),输出 ...

  7. LeetCode 314. Binary Tree Vertical Order Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...

  8. boost_1.61.0编译安装

    1.下载源码boost_1_61_0.zip 2.进入目录 C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\Shor ...

  9. Python numpy函数:transpose()

    transpose用于对高维数组进行转置,转置时候需要一个由轴编号组成的元组. 比如说三维的数组,那就对维度进行编号,也就是0,1,2:这样说可能比较抽象.这里的0,1,2可以理解为对shape返回元 ...

  10. 获取APK的package名和activity名

    使用 aapt dump badging + 需要安装的APK