数据结构(C#)--单链表
来源:互联网 发布:怎么下载气象数据 编辑:程序博客网 时间:2024/04/29 16:36
2007年最后一个月在忙碌度过了,最近在网上下了一本C#版本的数据结构的电子书,正好我也打算在把数据结构在过一遍,刚好可以看这本,还可以提高一下英文的阅读能力。
书名是《DATA STRUCTURES AND ALGORITHMS USING C#》,作者:MICHAEL MCMILLAN
以后我会陆续把这本数的心得发上来,同时也把自己亲手实践过的代码发上来,大家一起学习,一起进步。如果有什么错误,大家可以发邮件或者留言给我。谢谢。由于该书的结构和我们一般教材的结构不是很一样,所以我按照一般教材的顺序,先从链表开始,原书链表是在第十一章才讲的。书中代码有问题的地方我会在注释中标注出来。
namespace DataStruct
...{
//定义单链表的表头
public class Node
...{
//存储数据,定义为基类,可以存不同类型的数据
public Object Element;
//指向下一个结点的引用
public Node Link;
//构造空结点
public Node()
...{
this.Element = null;
this.Link = null;
}
//带参数的构造器
public Node(Object element)
...{
this.Element = element;
this.Link = null;
}
}
public class LinkedList
...{
//此处原书有误,不应该设置为peotected,这样会导致结点没有实例化
public Node Header;
public LinkedList()
...{
Header = new Node("header");
}
//查找链表中的元素
private Node Find(Object item)
...{
Node Current = new Node();
Current = Header;
//书中此处代码有误
//while(Current.header !=item)
while (Current.Element != item)
...{
Current = Current.Link;
}
return Current;
}
//在链表中插入元素
public void InsertNode(Object newItem,Object after)
...{
Node Current = new Node();
Node NewNode = new Node(newItem);
Current = Find(after);
if (Current != null)
...{
NewNode.Link = Current.Link;
Current.Link = NewNode;
}
}
public Node FindPrevious(Object n)
...{
Node Current = Header;
while (!(Current.Link == null) && (Current.Link.Element != n))
...{
Current = Current.Link;
}
return Current;
}
//删除结点
public void Remove(Object item)
...{
Node P = FindPrevious(item);
if (!(P.Link == null))
...{
P.Link = P.Link.Link;
}
}
//打印链表
public void PrintList()
...{
Node Current = new Node();
Current = this.Header;
while (Current.Link != null)
...{
Console.WriteLine(Current.Link.Element);
Current = Current.Link;
}
}
}
class Program
...{
static void Main(string[] args)
...{
//实例化结点
Node FirstNode = new Node("Tommy");
Node SecondNode = new Node("weiwei");
Int32 Num = 5;
//因为我们定义结点的时候是用object类型,所以结点可以存储不同类型
Node ThirdNode = new Node(Num);
FirstNode.Link = SecondNode;
SecondNode.Link = ThirdNode;
ThirdNode.Link = null;
LinkedList MyList = new LinkedList();
//将头结点指向第一个结点
MyList.Header.Link = FirstNode;
//插入结点
MyList.InsertNode("advantech", "weiwei");
MyList.InsertNode("插入链表", "Tommy");
//打印链表中的结点元素
MyList.PrintList();
Console.ReadLine();
}
}
}
...{
//定义单链表的表头
public class Node
...{
//存储数据,定义为基类,可以存不同类型的数据
public Object Element;
//指向下一个结点的引用
public Node Link;
//构造空结点
public Node()
...{
this.Element = null;
this.Link = null;
}
//带参数的构造器
public Node(Object element)
...{
this.Element = element;
this.Link = null;
}
}
public class LinkedList
...{
//此处原书有误,不应该设置为peotected,这样会导致结点没有实例化
public Node Header;
public LinkedList()
...{
Header = new Node("header");
}
//查找链表中的元素
private Node Find(Object item)
...{
Node Current = new Node();
Current = Header;
//书中此处代码有误
//while(Current.header !=item)
while (Current.Element != item)
...{
Current = Current.Link;
}
return Current;
}
//在链表中插入元素
public void InsertNode(Object newItem,Object after)
...{
Node Current = new Node();
Node NewNode = new Node(newItem);
Current = Find(after);
if (Current != null)
...{
NewNode.Link = Current.Link;
Current.Link = NewNode;
}
}
public Node FindPrevious(Object n)
...{
Node Current = Header;
while (!(Current.Link == null) && (Current.Link.Element != n))
...{
Current = Current.Link;
}
return Current;
}
//删除结点
public void Remove(Object item)
...{
Node P = FindPrevious(item);
if (!(P.Link == null))
...{
P.Link = P.Link.Link;
}
}
//打印链表
public void PrintList()
...{
Node Current = new Node();
Current = this.Header;
while (Current.Link != null)
...{
Console.WriteLine(Current.Link.Element);
Current = Current.Link;
}
}
}
class Program
...{
static void Main(string[] args)
...{
//实例化结点
Node FirstNode = new Node("Tommy");
Node SecondNode = new Node("weiwei");
Int32 Num = 5;
//因为我们定义结点的时候是用object类型,所以结点可以存储不同类型
Node ThirdNode = new Node(Num);
FirstNode.Link = SecondNode;
SecondNode.Link = ThirdNode;
ThirdNode.Link = null;
LinkedList MyList = new LinkedList();
//将头结点指向第一个结点
MyList.Header.Link = FirstNode;
//插入结点
MyList.InsertNode("advantech", "weiwei");
MyList.InsertNode("插入链表", "Tommy");
//打印链表中的结点元素
MyList.PrintList();
Console.ReadLine();
}
}
}
- 数据结构(C#)--单链表
- 数据结构(C#)--单链表
- c 数据结构 单链表
- 数据结构---单链表c实现
- 【数据结构(C++)】单链表
- 数据结构 - 单链表(C++)
- C数据结构之单链表
- C数据结构 单链表操作
- 数据结构 单链表 (C++)
- <C/C++数据结构>单链表
- 数据结构--单链表(C语言)
- C语言数据结构-单链表
- 数据结构----单链表(c++)
- [C++]数据结构之单链表
- 数据结构--单链表C实现
- 数据结构 单链表运用 C语言
- 数据结构 - 反转单链表(C++)
- 数据结构之单链表(C++)
- 今天有些感触
- c++字符串处理函数
- c++中使用 extern "c"
- C++中extern “C”含义深层探索
- c语言中长度为0的数组妙用
- 数据结构(C#)--单链表
- 和Windows作斗争:机器为何变慢了(3)?
- 看孩子
- NDS开发日志(一)
- 在ASP.NET 2.0中使用样式、主题和皮肤
- J.TEST"日语托业"高分四大秘诀
- [随记]被区分对待Manager和Service层头疼了两天
- 巨人史玉柱经典创业语录
- Hot Tech in Future