internal sealed class Node<T> { public T m_data; public Node<T> m_next; public Node(T data) : this(data, null) { } public Node(T data, Node<T> next) { m_data = data; m_next = next;//存储上一个Node<T>结构 } public override string ToString() { return m_data.ToString() + ((m_next != null) ? m_next.ToString() : null); } public static void SameDataLinkedList() { Node<char> head = new Node<char>('C'); head = new Node<char>('B', head); head = new Node<char>('A', head); Console.WriteLine(head.ToString()); } } }
同时存储多个类型的链表
internal class Node { protected Node m_next; public Node(Node next) { m_next = next; } } internal sealed class TypeNode<T> : Node { public T m_data; public TypeNode(T data) : this(data, null) { } public TypeNode(T data, Node next) : base(next) { m_data = data; } public override string ToString() { return m_data.ToString() + ((m_next != null) ? m_next.ToString() : null); } public static void SameDataLinkedList() { Node head = new TypeNode<char>('.'); head = new TypeNode<DateTime>(DateTime.Now, head); head = new TypeNode<string>("Today to", head); Console.WriteLine(head.ToString()); } }