Loading

C# 链表操作

关于链表操作,在C#当中微软已经提供了一个LinkedList<T>的数据结构,通过这个类提供的一系列方法就能够实现链表操作。

这里我提供一段代码,这是在论坛里面有人提问时给出的代码,它实现了自定义链表的操作(读者可以在此基础上进一步完善)。因为这段代码涉及一些C#技巧,所以贴出来给初学者学习C#提供一点参考。

实体类:

    /// <summary>
    /// 学生类
    /// </summary>
    public class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public Student(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }

        public override string ToString()
        {
            return "\r\n" + this.Name + ":年龄" + this.Age + "";
        }
    }

链表节点类:

    /// <summary>
    /// 节点类
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    public class Node<T>
    {
        public T Data { get; set; }
        public Node<T> Next { get; set; }

        public Node(T data)
        {
            this.Data = data;
            this.Next = null;
        }

        /// <summary>
        /// 附加节点
        /// </summary>
        /// <param name="newNode">新的节点</param>
        public void Append(Node<T> newNode)
        {
            //如果下一节点为null,则将新的节点指向下一节点
            if (this.Next == null)
            {
                this.Next = newNode;
            }
            //如果下一节点不为null,则直接附加到下一节点
            else
            {
                this.Next.Append(newNode);
            }
        }

        public override string ToString()
        {
            string output = this.Data.ToString();
            if (this.Next != null)
            {
                output += " " + this.Next.ToString();
            }
            return output;
        }
    }

链表类:

    /// <summary>
    /// 链表类
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    public class LinkedList<T>
    {
        Node<T> headNode = null;//头节点

        /// <summary>
        /// 追加节点方法
        /// </summary>
        /// <param name="data"></param>
        public void Add(T data)
        {
            if (headNode == null)
            {
                headNode = new Node<T>(data);
            }
            else
            {
                headNode.Append(new Node<T>(data));
            }
        }

        /// <summary>
        /// 索引器,通过索引获取节点
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T this[int index]
        {
            get
            {
                int temp = 0;
                Node<T> node = headNode;

                while (node != null && temp <= index)
                {
                    if (temp == index)
                    {
                        return node.Data;
                    }
                    else
                    {
                        node = node.Next;
                    }
                    temp++;
                }
                return default(T);
            }
        }

        public override string ToString()
        {
            if (headNode != null)
            {
                return this.headNode.ToString();
            }
            return string.Empty;
        }
    }

主函数:

    class Program
    {
        static void Main(string[] args)
        {
            LinkedList<int> intList = new LinkedList<int>();
            Enumerable.Range(0, 5).ToList<int>().ForEach(x => intList.Add(x));
            Console.WriteLine("整型的内容为:{0}\r\n", intList);

            LinkedList<Student> students = new LinkedList<Student>();
            students.Add(new Student("张三", 20));
            students.Add(new Student("李四", 22));
            students.Add(new Student("王五", 21));
            Console.WriteLine(students[1].Name + "的年龄为:" + students[1].Age);

            Console.ReadLine();
        }
    }

最后附录一下微软官方的链表类。https://msdn.microsoft.com/zh-cn/library/he2s3bh7(v=vs.110).aspx

posted @ 2015-03-13 14:05  guwei4037  阅读(2657)  评论(0编辑  收藏  举报