Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericA
{
public class GenericList<T>
{
/// <summary>
/// 节点类
/// </summary>
private class Node
{
private Node next;//下一个
public Node Next
{
set { next = value; }
get { return next; }
}
private T date;//包含的数据
public T Data
{
set { date = value; }
get { return date; }
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="t"></param>
public Node(T t)
{
next = null;
date = t;
}
}
/// <summary>
/// 头节点
/// </summary>
private Node head;
/// <summary>
/// 构造函数
/// </summary>
public GenericList()
{
head = null;
}
/// <summary>
/// 添加节点信息
/// </summary>
/// <param name="t"></param>
public void AddHead(T t)
{
Node n = new Node(t);
n.Next = head;
head = n;
}
/// <summary>
/// 实现递归
/// </summary>
/// <returns></returns>
public IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
}
class Program
{
static void Main(string[] args)
{
GenericList<int> list = new GenericList<int>();
for (int x = 0; x < 10; x++)
{
list.AddHead(x);
}
foreach (int i in list)
{
System.Console.WriteLine(i+" ");
} Console.WriteLine("\nDone!");
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericA
{
public class GenericList<T>
{
/// <summary>
/// 节点类
/// </summary>
private class Node
{
private Node next;//下一个
public Node Next
{
set { next = value; }
get { return next; }
}
private T date;//包含的数据
public T Data
{
set { date = value; }
get { return date; }
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="t"></param>
public Node(T t)
{
next = null;
date = t;
}
}
/// <summary>
/// 头节点
/// </summary>
private Node head;
/// <summary>
/// 构造函数
/// </summary>
public GenericList()
{
head = null;
}
/// <summary>
/// 添加节点信息
/// </summary>
/// <param name="t"></param>
public void AddHead(T t)
{
Node n = new Node(t);
n.Next = head;
head = n;
}
/// <summary>
/// 实现递归
/// </summary>
/// <returns></returns>
public IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
}
class Program
{
static void Main(string[] args)
{
GenericList<int> list = new GenericList<int>();
for (int x = 0; x < 10; x++)
{
list.AddHead(x);
}
foreach (int i in list)
{
System.Console.WriteLine(i+" ");
} Console.WriteLine("\nDone!");
Console.ReadKey();
}
}
}