栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#避免回溯方法心得

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#避免回溯方法心得

本文实例讲述了C#避免回溯方法,分享给大家供大家参考之用。具体分析如下:

首先,回溯法是不可控的,有时候会超出我们意料之外产生不妙的结果,最常见的也就是内存泄漏。。

回溯方法是很容易想到,又不容易想到的,往往,我们思维更容易进入的是回溯法。但是回溯法有着它的弊端,非常明显的弊端是作用域内产生的变量和引用在回溯法调用未完成时,不能释放(对于大部分编辑器来说,排除有着优化能力的编辑器)。如果我们在某一方法中使用极多的回溯调用,在方法中不能及时的对方法作用域内的变量和引用释放,最终会造成内存不足和cpu的计算负荷增大(内存机制中可以将过剩的数据转存到虚拟内存、硬盘,这个就不说了)。使用栈(队)式的循环,可以轻易避免回溯法,而且栈(队)式的数据再使用之后可以很方便的抛出移除。某些时候就是这样,一个小小的改动,可以让一个程序在某种特定的环境中起死回生。(之前做过一个数独运算器的算法,后来的优化改进就是为了避免回溯)

示例代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace 避免回溯方法
{
  class Program
  {
    static void Main(string[] args)
    {
      string path = AppDomain.CurrentDomain.baseDirectory;

      List fileList1 = new List();
      FunctionHuishuo(path, ref fileList1);

      List fileList2 = new List();
      FunctionQueue(path, ref fileList2);

      List fileList3 = new List();
      FunctionStack(path, ref fileList3);
    }

    /// 
    /// 回溯法
    /// 
    /// 
    /// 
    private static void FunctionHuishuo(string path, ref List fileList)
    {
      if (true)
      {
 string[] files = null;
 try
 {
   files = Directory.GetFiles(path);
 }
 catch { }

 if (files != null && files.Length > 0)
 {
   fileList.AddRange(files);
 }
      }

      if (true)
      {
 string[] folders = null;
 try
 {
   folders = Directory.GetDirectories(path);
 }
 catch { }

 if (folders != null && folders.Length > 0)
 {
   foreach (string folder in folders)
   {
     FunctionHuishuo(folder, ref fileList);
   }
 }
      }
    }

    /// 
    /// 堆栈法
    /// 
    /// 
    private static void FunctionStack(string path, ref List fileList)
    {
      Stack stack = new Stack();
      stack.Push(path);

      while (stack.Count > 0)
      {
 string dir = stack.Pop();

 string[] files = null;
 try
 {
   files = Directory.GetFiles(dir);
 }
 catch { }

 if (files != null && files.Length > 0)
 {
   fileList.AddRange(files);
 }

 string[] folders = null;
 try
 {
   folders = Directory.GetDirectories(dir);
 }
 catch { }

 if (folders != null && folders.Length > 0)
 {
   foreach (string folder in folders)
   {
     stack.Push(folder);
   }
 }
      }
    }

    /// 
    /// 队列法
    /// 
    /// 
    private static void FunctionQueue(string path, ref List fileList)
    {
      Queue queue = new Queue();

      queue.Enqueue(path);

      while (queue.Count > 0)
      {
 string dir = queue.Dequeue();

 string[] files = null;
 try
 {
   files = Directory.GetFiles(dir);
 }
 catch { }

 if (files != null && files.Length > 0)
 {
   fileList.AddRange(files);
 }

 string[] folders = null;
 try
 {
   folders = Directory.GetDirectories(dir);
 }
 catch { }

 if (folders != null && folders.Length > 0)
 {
   foreach (string folder in folders)
   {
     queue.Enqueue(folder);
   }
 }
      }
    }
  }
}

请仔细对比下三种循环结构的写法,特别注意下里面有用到  if(true){...}   ,这种方式在某些编辑环境中可以产生非常美妙的效果。

相信本文所述对大家C#程序设计的学习有一定的借鉴价值。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/126070.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号