读书人

C# 写的一颗树(不知有没有内存储器

发布时间: 2013-02-02 12:27:16 作者: rapoo

C# 写的一颗树,(不知有没有内存卸漏?)
请大侠指出下面的代码有没有问题!
主要是删除节点时内存会不会有卸漏?


//节点数据//
public class TreeContainerNode
{

object data;
TreeContainerNode parent;
List<TreeContainerNode> childrens;

public object Data
{
get { return data; }
set { data = value; }
}
public TreeContainerNode Parent
{
get { return parent; }
set { parent = value; }
}
public List<TreeContainerNode> Childrens
{
get { return childrens; }
set { childrens = value; }
}

public TreeContainerNode(object data, TreeContainerNode parentNode, List<TreeContainerNode> children)
{
this.data = data;
this.parent = parentNode;
this.childrens = children;
}


}
//树型容器,树型 数据结构
public class TreeContainer
{
//
TreeContainerNode headNode;

public TreeContainerNode HeadNode
{
get { return headNode; }
set { headNode = value; }
}
//指针//
private TreeContainerNode current;//当前节点
//构造 器
public TreeContainer(object data)
{
this.headNode = new TreeContainerNode(data, null, null);
this.current = this.headNode;
}
//移到上一节点
public bool MoveToParent()
{
if (current.Parent == null)
{
return false;
}
else
{
this.current = this.current.Parent;//移动
return true;
}
}
//移到子节点
///添
public bool MoveToChildren(int index)
{
if (this.current.Childrens == null)//没有子节点
{
return false;
}
if (this.current.Childrens.Count <= index)//没有这么多子节点
{
return false;
}
else
{
this.current = this.current.Childrens[index];
}
}
//添加子节点
public bool AddChildrenNode(object data)
{
//设为头节点
if (this.headNode == null)
{
this.headNode = new TreeContainerNode(data, null, null);
return false;
}
else
{
if (this.current.Childrens == null)//表示没有子节点
{
//创建子节点的容器
this.current.Childrens = new List<TreeContainerNode>();
//添加子节点数据
this.current.Childrens.Add(new TreeContainerNode(data, this.current, null));
}
else//表示 有字节点
{
this.current.Childrens.Add(new TreeContainerNode(data, this.current, null));
}
return true;
}
}
///删
//删除当前节点
public bool DeleteCurrentNode()
{
//没有父节点 当前节点就是根节点
//不能删除跟节点
//要删除,可以销毁本对象
if (this.current.Parent == null)
{
return false;
}
else
{
//删除并移到父节点
//记录
TreeContainerNode nodeRecord = this.current.Parent;
//删除
this.current = null;
GC.Collect();//回收!
//还原
this.current = nodeRecord;//
}
}
//册除对象的子节点
public bool DeleteChildrens()
{
if (this.current.Childrens == null)


{
return false;
}
else
{
//不知这样是否会有内存泄漏,因为他的字节点可能来用字节点!
this.current.Childrens == null;
GC.Collect();
return true;
}
}
///改
public bool AlterCurrentData(object data)
{
//正常情况下,不会执行下面的代码
if (this.current == null)
{
return false;
}
//
this.current.Data = null;
GC.Collect();
this.current.Data = data;
return true;
}
///查
//查当前指针指向的数据
public object GetCurrentData()
{
if (this.current == null)
{
return null;
}
return this.current.Data;
}
//查当前字节点的数据
public List<object> GetChildrenData()
{
if (this.current.Childrens == null || this.current.Childrens.Count <= 0)
{
return null;
}
else
{
List<object> data = new List<object>();
foreach (TreeContainerNode node in this.current.Childrens)
{
data.Add(node.Data);
}
return data;
}
}
}


c# object list 数据结构
[解决办法]
this.current?=?null;
GC.Collect();//回收!
这2行多余,直接将当前节点修改即可,无需指向null,更不用GC来回收,影响效率的。
.NET除非引用存在,或者使用了托管资源,否则不可能内存泄漏,只要对象引用没了(没有变量指向它),就肯定会被回收的。

读书人网 >C#

热点推荐