Customize a linked list that can receive different data types and Custom Data Types

Source: Internet
Author: User

Customize a linked list that can receive different data types and Custom Data Types

In C #, applicable to linked list scenariosLinkedList,LinkedListIt can be considered as a set. The type of the Set element isLinkedListNode, Which is different from other set elements:LinkedListNodeNot only store its own values, but also include references to the previous node and the next node. DecompilationLinkedListNodeYou can see the following:

 

public sealed class LinkedListNode<T>{
    internal T item;
    internal LinkedListNode<T> next;
    internal LinkedListNode<T> prev;

    ......
}

 

UseLinkedListAs follows:

 

        static void Main(string[] args)
        {
            LinkedList<string> strs = new LinkedList<string>();
            strs.AddFirst("b");
            strs.AddFirst("a");

            foreach (var item in strs)
            {
                Console.Write(item + " ");
            }
            Console.ReadKey();
        }

 

Above,LinkedListInLinkedListNodeThe Data Types of nodes areLinkedListNodeHowever, ifLinkedListNodeWhat should I do if the data types are different?

 

We customize a linked list node so that it can receive different types of data. In a user-defined linked list node, the Data Types of each node may be different, but in this linked list node, the attribute types pointing to the next linked list node are the same, you can first extract a linked list node base class:

 

    public class Node
    {
        protected Node _next;

        public Node(Node next)
        {
            _next = next;
        }
    }

 

Represents the generic class of the linked list node to implement this base class.

 

    public class MyNode<T> : Node
    {
        public T _data;

        public MyNode(T data, Node next) : base(next)
        {
            _data = data;
        }

        public MyNode(T data) : this(data, null)
        {
            
        }

        public override string ToString()
        {
            return _data.ToString() + " " + ((_next != null) ? _next.ToString() : "");
        }
    }    


The following code is called on the client:

 

        static void Main(string[] args)
        {
            Node node = new MyNode<DateTime>(DateTime.Now);
Node = new MyNode <string> ("the current time is", node );
            Console.WriteLine(node.ToString());
            Console.ReadKey();
        }

Summary: generic classes are also classes that can be derived from a specific class. The above, with the helpMyNodeTo receive different data typesMyNodeParent classNodeAbstract The next node reference.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.