泛型链表结构
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了泛型链表结构相关的知识,希望对你有一定的参考价值。
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()); } }
以上是关于泛型链表结构的主要内容,如果未能解决你的问题,请参考以下文章
C语言 泛型链表 如何计算(结构体中各元素)相对内存地址?(字节对齐,结构体对齐)offsetof()函数 & ( (struct X*)0 ) -> Y)语法