首页 > 其他 > 详细

泛型链表结构

时间:2016-11-24 06:51:12      阅读:179      评论:0      收藏:0      [点我收藏+]
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());
        }

    }

 

泛型链表结构

原文:http://www.cnblogs.com/Tan-sir/p/6035873.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!