using System;
using System.Collections.Generic;
using System.Collections;
namespace MyPractise.LinkPractise
{
class Program
{
static void Main()
{
//Initialize a person link instance
LinkList<Person> personLink = new LinkList<Person>();
//Add 50 persons to the link by the method AddFirstNode
for (int i = 0; i < 5; i++)
{
Person person = new Person(i.ToString(), string.Format("Alias{0}", i.ToString()));
personLink.AddFirstNode(person);
}
//Add 50 persons to the link by the method AddLastNode
for (int i = 5; i < 10; i++)
{
Person person = new Person(i.ToString(), string.Format("Alias{0}", i.ToString()));
personLink.AddLastNode(person);
}
//Output the infor of the 100 persons
foreach (Person person in personLink)
{
Console.WriteLine(string.Format("Name:{0},Alias:{1}",person.Name,person.Alias));
}
Console.ReadKey();
return;
}
}
#region Node example class
/// <summary>
/// Node example class
/// </summary>
class Person : Node<Person>
{
public string Name { get; private set; }
public string Alias { get; private set; }
public Person(string name, string alias)
{
Name = name;
Alias = alias;
}
}
#endregion
#region Basic Classes
/// <summary>
/// Node template of the link
/// </summary>
/// <typeparam name="T">T inherited from Node<T></typeparam>
abstract class Node<T> where T:Node<T>
{
/// <summary>
/// Reserve next node
/// </summary>
public T Next { get; set; }
}
/// <summary>
/// Link
/// </summary>
/// <typeparam name="T">Node of the link</typeparam>
class LinkList<T> where T:Node<T>
{
/// <summary>
/// First node of the link
/// </summary>
public T FirstNode { get; private set; }
/// <summary>
/// Last node of the link
/// </summary>
public T LastNode { get; private set; }
/// <summary>
/// Use foreach cycle to output all the node
/// </summary>
/// <returns></returns>
public IEnumerator GetEnumerator()
{
T current=FirstNode;
while (current != null)
{
yield return current;
current = current.Next;
}
}
/// <summary>
/// Add node to the first of the link
/// </summary>
/// <param name="newFirstNode">Node instance</param>
public void AddFirstNode(T newFirstNode)
{
if (FirstNode == null)
{
FirstNode = newFirstNode;
LastNode = FirstNode;
}
else
{
newFirstNode.Next = FirstNode;
FirstNode = newFirstNode;
}
}
/// <summary>
/// Add node to the last of the link
/// </summary>
/// <param name="newLastNode">Node instance</param>
public void AddLastNode(T newLastNode)
{
if (LastNode == null)
{
LastNode = newLastNode;
FirstNode = LastNode;
}
else
{
LastNode.Next = newLastNode;
LastNode = newLastNode;
}
}
#endregion
}
}
分享到:
相关推荐
c#链表实现求两个多项式的和,例子比较简单,有些功能还没有实现,请谅解
本来是自己有一个小东西需要链表存储,从网上找的资源,发现有bug,该资源bug已修复
c#单向链表的实现,帮助大家学习,程序完全可以运行,
C#单向链表的实现的源码
不使用List,使用泛型类实现单链的增删。
该代码应用到了模板、接口、结构体等,具有较好的复用性
实现双向链表,符合双向链表的思路且易懂,拓展方便。
此表现形式符合单向链表的思路,设计清晰。拓展方法时在CList类里写。
C#双向链表的实现的源码
集合了对C#单向链表的实现。让大家熟悉对C#操作
数据结构实现方式,可以更深刻的理解这些数据结构的实现方式
( 【C#实例】链表实现自制List类
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运...
阶段练习:实现链表(LinkedList) 简介:写一个链表的数据结构,要求实现IList接口。 具体要求: 1、 使用代码规范。 2、 至少对IList中的Add,Remove,Insert,Indexer,IEnumerator进行单元测试。 3、 对上述每个...
一、关于C#链表 C#链表可用类LinkedList来存放。本文中的类MyLinkedList只是实现了该类的最基本的功能。C#中没有指针,但因为C#中类在赋值时传递的是地址,因此仍然可以利用这点制作一个链表。 二、结点类Node和链表...
本文实例讲述了C#通过链表实现队列的方法。分享给大家供大家参考。具体实现方法如下: public class Node { public int Data { get; set; } public Node Next { get; set; } public Node(int data) { this....
采用双向链表实现的AOI,可支持大量实体的范围检测,实现了进入AOI和离开AOI采用不同的半径
操作系统c++编程实现安全型双向链表,线程的创建,利用线程对链表进行增删改操作,并检验结果是否正确
本篇文章是对c#中自定义泛型链表类进行了详细的分析介绍,需要的朋友参考下
包含方法: 链表的节点类(Node), 链表的是否为空, 输出链表数据, 链表的Add方法, 链表的Remove方法, 链表的串联方法, 链表的反转