C#它配备了一个泛型列表类,在很多情况下,足以。实际应用中遇到。最好的报价C#该链表,包装成自己的阶级需求。

该名单的努力的原则,基本实现探索实施一些简单的方法。

一个、(Node.cs文件)作为一类节点列表。它被封装在内部列表类。

原因是客户在使用链表时不须要关注链表的节点类型。

<span style="font-family:Courier New;">using System;

namespace LinkListTrial0._2
{
partial class MyLinkedList<T>
{
class Node<DataType>:IComparable<DataType>,IEquatable<DataType>
where DataType:IComparable
{
DataType data;
Node<DataType> next;
public Node<DataType> Next
{
get
{
return this.next;
}
set
{
this.next = value;
}
} public Node(DataType data)
{
this.data = data;
this.next = null;
} // 比較接口的实现,直接比較链表节点与数据
public int CompareTo(DataType obj)
{
DataType objData = (DataType)obj;
return this.data.CompareTo(objData);
} // 相等接口的实现,直接比較链表节点与数据
public bool Equals(DataType to_compare)
{
return this.data.Equals(to_compare);
} // 打印数据
public override string ToString()
{
// TODO:能够加一些打印链接的创意
return this.data.ToString();
}
}
} }</span>

二、(MyLinkedList.cs文件)实现简单链表的类。存放实现链表的主要逻辑。

主要目的是实现简单链表的添加和删除逻辑。

<span style="font-family:Courier New;">using System;

namespace LinkListTrial0._2
{
partial class MyLinkedList<T>
where T:IComparable
{
Node<T> Head;
public MyLinkedList()
{
// 在链表头部放置一个虚拟节点。简化逻辑
Node<T> dummy = new Node<T>(default(T));
this.Head =dummy;
} public void AddAscend(T to_add)
{
Node<T> currentNode = Head;
Node<T> nextNode; // 升序排列数据
for ( nextNode = currentNode.Next ; nextNode!=null && nextNode.CompareTo(to_add) < 0; )
{
currentNode = nextNode;
nextNode = currentNode.Next;
} // 忽略反复数据
if (nextNode!=null && nextNode.Equals(to_add))
{
return;
} // 插入新来的非反复数据
Node<T> toaddNode = new Node<T>(to_add);
currentNode.Next = toaddNode;
toaddNode.Next = nextNode;
} public void Delete(T to_delete)
{
Node<T> currentNode = Head;
Node<T> nextNode; // 检測全部数据是否与待删除的数据相等
for (nextNode=currentNode.Next;!( nextNode==null || nextNode.Equals(to_delete)); )
{
currentNode = nextNode;
nextNode = currentNode.Next;
} // 从中间删除数据
if (nextNode != null)
{
currentNode.Next = nextNode.Next;
}
} public void Clear()
{
// 清空全部数据
this.Head.Next = null;
} public void PrintAll()
{
Node<T> currentNode = Head;
Node<T> nextNode; for (nextNode=currentNode.Next; nextNode!=null; )
{
Console.WriteLine(nextNode.ToString());
currentNode = nextNode;
nextNode = currentNode.Next;
}
}
}
}</span>

三、(Program.cs文件)简单的測试程序。

<span style="font-family:Courier New;">using System;

namespace LinkListTrial0._2
{
class Program
{
static void Main(string[] args)
{
MyLinkedList<int> list = new MyLinkedList<int>();
list.Clear();
list.AddAscend(1);
list.AddAscend(6);
list.AddAscend(1);
list.AddAscend(3);
list.AddAscend(4);
list.AddAscend(2);
list.AddAscend(5);
list.AddAscend(6);
list.PrintAll(); Console.WriteLine("----------------------------------------------"); list.Delete(6);
list.Delete(7);
list.PrintAll(); Console.ReadLine(); }
}
}</span>

四、小结

1、实现了简单的列表。

2、没有考虑性能的因素。

3、数据是相对固定的,应该依据使用情况灵活调整数据结构。

版权声明:本文博客原创文章,博主不同意(mailto:cqwd2010@qq.com)版权所有。

最新文章

  1. Git从码云Clone代码到本地
  2. CentOS配置git和maven自动部署java
  3. win10 右键菜单添加使用gvim打开方式
  4. spring依赖注入单元测试:expected single matching bean but found 2
  5. Scrum角色
  6. Hibernate逍遥游记-第3章对象-关系映射基础-access=&quot;field&quot;、dynamic-insert、dynamic-update、formula、update=false
  7. Linux中的栈:用户态栈/内核栈/中断栈
  8. Porsche Piwis Tester II “No VCI has been detected”,how to do?
  9. [TCO 2012 Round 3A Level3] CowsMooing (数论,中国剩余定理,同余方程)
  10. 打开局域网项目,显示“项目位置不受信任”的解决办法(VS2008)
  11. javascript特效:会随着鼠标而动的眼睛
  12. The method getDispatcherType() is undefined for the type HttpServletRequest 升级到tomcat8(转)
  13. Spring Boot 系列教程4-JDBC
  14. 学习animate.css包含了一组炫酷、有趣、跨浏览器的动画
  15. bootstrap-treeview 如何实现全选父节点下所有子节点及反选
  16. 【转】C++标准转换运算符reinterpret_cast
  17. Linux/unix 查看端口占用
  18. Go命令行参数解析flag包
  19. 《剑指offer》青蛙跳台阶
  20. V4 V7 V13支持包的区别(转)

热门文章

  1. Hibernate 配置详解(11)
  2. Oracle SQL Developer使用
  3. Android学习之 AChartEngine 图表绘制
  4. DLNA它 Error, can&amp;#39;t findlibavformat ! 解
  5. HOJ2275 Number sequence
  6. 【剑指offer】设置在最小数目的阵列
  7. 同一个存储过程中,不能多次select into 到同一张表的问题
  8. HTML学习_01
  9. BootStrap -- Grid System
  10. Eclipse在Jar形成和应用程序包