C# 如何在c语言中使单链表通用#

C# 如何在c语言中使单链表通用#,c#,generics,C#,Generics,我有一个单链表的实现,它可以与int配合使用。我想让它成为无继承的泛型。我的测试数据是电信号,我需要测量填充的执行时间。我不需要添加任何新方法,一切都很好,只需要将其作为模板/泛型。我怎样才能做到这一点?谢谢 这是我的密码 public class LinkedList { //structure private Node head; private int count; public LinkedList()

我有一个单链表的实现,它可以与
int
配合使用。我想让它成为无继承的泛型。我的测试数据是电信号,我需要测量填充的执行时间。我不需要添加任何新方法,一切都很好,只需要将其作为模板/泛型。我怎样才能做到这一点?谢谢

这是我的密码

public class LinkedList
    {
        //structure
        private Node head;
        private int count;

        public LinkedList()
        {
            //constructor
        }

        public bool IsEmpty
        {
            //check if list is empty or not
        }

        public int Count
        {
            //count items in the list
        }

        public object Add(int index, object o)
        {
            //add items to the list from beginning/end  
        }

        public object Delete(int index)
        {
            //delete items of the list from beginning/end   
        }

        public void Clear()
        {
            //clear the list    
        }
    }
  • 将链表元素类型的所有实例从
    int
    更改为
    T
    。(不要盲目地更改所有
    int
    ——只更改用于保存元素的元素。因此不要更改
    count
    index
    ,例如!)
  • 将类声明更改为
    公共类链接列表
  • 尝试编译它并修复任何错误
  • 更新单元测试,使它们能够编译,并确保它们仍然通过。(您的链表确实有单元测试,对吗?;)
  • 我不确定您的
    对象
    参数。也许它们也应该改为
    T

    您没有显示
    节点
    实现,但我猜您必须为此执行类似的操作:使其成为
    节点

  • 将链表元素类型的所有实例从
    int
    更改为
    T
    。(不要盲目地更改所有
    int
    ——只更改用于保存元素的元素。因此不要更改
    count
    index
    ,例如!)
  • 将类声明更改为
    公共类链接列表
  • 尝试编译它并修复任何错误
  • 更新单元测试,使它们能够编译,并确保它们仍然通过。(您的链表确实有单元测试,对吗?;)
  • 我不确定您的
    对象
    参数。也许它们也应该改为
    T


    您没有显示
    节点
    实现,但我猜您必须为此执行类似的操作:使其成为
    节点
    等。

    您的LinkedList应该如下所示

    public class LinkedList<T>
    {
        public class Node<T>
        {
            public T data;
            public Node<T> next;
        }
    
        //structure
        private Node<T> head;
        private int count;
    
        public LinkedList()
        {
            //constructor
        }
    
        public bool IsEmpty
        {
            //check if list is empty or not
        }
    
        public int Count
        {
            //count items in the list
        }
    
        public T Add(int index, T o)
        {
            //add items to the list from beginning/end  
        }
    
        public T Delete(int index)
        {
            //delete items to the list from beginning/end   
        }
    
        public void Clear()
        {
            //clear the list    
        }
    }
    
    公共类链接列表
    {
    公共类节点
    {
    公共数据;
    公共节点下一步;
    }
    //结构
    专用节点头;
    私人整数计数;
    公共链接列表()
    {
    //建造师
    }
    公共图书馆是空的
    {
    //检查列表是否为空
    }
    公共整数计数
    {
    //对列表中的项目进行计数
    }
    公共T添加(整数索引,TO)
    {
    //从开始/结束向列表中添加项目
    }
    公共T删除(整数索引)
    {
    //从开始/结束删除列表中的项目
    }
    公共空间清除()
    {
    //清除列表
    }
    }
    
    您的LinkedList应该如下所示

    public class LinkedList<T>
    {
        public class Node<T>
        {
            public T data;
            public Node<T> next;
        }
    
        //structure
        private Node<T> head;
        private int count;
    
        public LinkedList()
        {
            //constructor
        }
    
        public bool IsEmpty
        {
            //check if list is empty or not
        }
    
        public int Count
        {
            //count items in the list
        }
    
        public T Add(int index, T o)
        {
            //add items to the list from beginning/end  
        }
    
        public T Delete(int index)
        {
            //delete items to the list from beginning/end   
        }
    
        public void Clear()
        {
            //clear the list    
        }
    }
    
    公共类链接列表
    {
    公共类节点
    {
    公共数据;
    公共节点下一步;
    }
    //结构
    专用节点头;
    私人整数计数;
    公共链接列表()
    {
    //建造师
    }
    公共图书馆是空的
    {
    //检查列表是否为空
    }
    公共整数计数
    {
    //对列表中的项目进行计数
    }
    公共T添加(整数索引,TO)
    {
    //从开始/结束向列表中添加项目
    }
    公共T删除(整数索引)
    {
    //从开始/结束删除列表中的项目
    }
    公共空间清除()
    {
    //清除列表
    }
    }
    
    将类声明为
    LinkedList
    ,其中
    T
    是泛型类型,然后修改
    Add
    方法以接受类型为
    T
    的对象:
    公共对象添加(int index,T元素)
    将类声明为
    LinkedList
    ,其中
    T
    是泛型类型,然后修改您的
    Add
    方法以接受类型为
    T
    的对象:
    public object Add(int index,T element)

    请发布一点列表实现的代码请发布一点列表实现的代码谢谢快速反馈@fuboThanks可提供快速反馈@伏波