用c#如何实现链式存储?比如说列表或树!!(用托管代码)

VICKYLAU 2003-08-26 06:21:59
急!!!!!请各位大虾指教!!!!!!
...全文
98 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
cppTrier 2003-08-26
  • 打赏
  • 举报
回复
这位是不是从C++转到C#上来的? C#里的reference type的变量储存是其实是对象的引用,也就是说可以相当于C++里指针来用。
像C++链表的
class node
{
int data;
node* next;
}
在C#里只要写成
class node
{
int data;
node next;
}
就可以了。
qqq123 2003-08-26
  • 打赏
  • 举报
回复
////////////////////////
//Tree
///////////////////////
using System;
using System.Collections;

namespace SampleTree
{
public class TreeNode
{
#region The TreeNodeCollection class.
public class TreeNodeCollection:ICollection
{
private TreeNode _owner=null;
private ArrayList _innerList=new ArrayList();

public TreeNodeCollection(TreeNode owner)
{
this._owner=owner;
}
public TreeNode this[int index]
{
get{return (TreeNode)this._innerList[index];}
set
{
if(value ==null)
throw new ArgumentNullException();
(this._innerList[index] as TreeNode)._parent=null;;
value._parent=this._owner;
this._innerList[index]=value;
}
}
public bool IsSynchronized
{
get{ return this._innerList.IsSynchronized;}
}
public int Count
{
get{ return this._innerList.Count;}
}
public void CopyTo(Array array, int index)
{
this._innerList.CopyTo(array,index);
}
public object SyncRoot
{
get
{
return this._innerList.SyncRoot;
}
}
public IEnumerator GetEnumerator()
{
return this._innerList.GetEnumerator();
}

public int Add(TreeNode value)
{
if(value ==null)
throw new ArgumentNullException();
value._parent=this._owner;
return this._innerList.Add(value);
}

public void Insert(int index, TreeNode value)
{
if(value ==null)
throw new ArgumentNullException();
value._parent=this._owner;
this._innerList.IndexOf(value,index);
}

public void RemoveAt(int index)
{
try
{
this[index]._parent=null;
this._innerList.RemoveAt(index);
}
catch
{
}
}
public int IndexOf(TreeNode value)
{
return this._innerList.IndexOf(value);
}
public void Clear()
{
for(int i=0;i<this.Count;i++)
{
this[i]._parent=null;
}
this._innerList.Clear();
}
public bool Contains(TreeNode value)
{
return this._innerList.Contains(value);
}
}
#endregion

private TreeNode _parent=null;
private string _value=null;
private TreeNodeCollection _childNodes=null;

public TreeNode(string value)
{
this._value=value;
}
public TreeNode ParentNode
{
get{return this._parent;}
}
public string Value
{
get{return this._value;}
}
public TreeNodeCollection ChildNodes
{
get
{
if(this._childNodes !=null) return this._childNodes;
this._childNodes=new TreeNodeCollection(this);
return this._childNodes;
}
}
public bool IsRoot
{
get{return this.ParentNode==null;}
}
public bool IsLeaf
{
get
{
return this._childNodes==null || this._childNodes.Count==0;
}
}
public string Location
{
get
{
string location=string.Empty;
TreeNode treeNode=this;
while(treeNode.ParentNode!=null)
{
treeNode=treeNode.ParentNode;
location=treeNode.Value+@"|"+location;
}
return this.Location;
}
}
}
public class Tree
{
private TreeNode.TreeNodeCollection _nodes=new TreeNode.TreeNodeCollection(null);

public TreeNode.TreeNodeCollection Nodes
{
get{return this._nodes;}
}
public TreeNode SearchNode(string location)
{
string[] loc=location.Split(new char[]{'|'});
if(loc.Length==0) return null;
TreeNode treeNode=null;
for(int i=0;i<this.Nodes.Count;i++)
{
if(this.Nodes[i].Value==loc[0])
{
treeNode=this.Nodes[i]; break;
}
}
int c=1;
while(treeNode!=null && c<loc.Length)
{
for(int i=0;i<treeNode.ChildNodes.Count;i++)
{
if(treeNode.ChildNodes[i].Value==loc[c])
{
treeNode=treeNode.ChildNodes[i];
c++;
}
}
treeNode=null;
}
return treeNode;
}
}
}

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧