zoukankan      html  css  js  c++  java
  • 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 "
    " + 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}
    ", 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

  • 相关阅读:
    windows安装pkg-config
    完美解决Cannot download "https://github.com/sass/node-sass/releases/download/binding.nod的问题
    iOS 解析RFC3339时间格式
    依赖工程开发,编译报错Command Libtool failed with a nonzero exit code
    移除项目中的UIWebView
    输出手机中的字体与常用的苹方字体名称
    Null passed to a callee that requires a non-null argument
    SDK内本地化处理 localizedStringForKey:value:table:
    iOS [AFHTTPSessionManager GET:parameters:progress:success:failure:]: unrecognized selector sent to
    xcode搜索路径缩写
  • 原文地址:https://www.cnblogs.com/guwei4037/p/4335003.html
Copyright © 2011-2022 走看看