• 泛型(三)泛型接口


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace  泛型接口
    {
        public class GenericList<T> : System.Collections.Generic.IEnumerable<T>
        {
            //字段
            protected Node head;
            protected Node current = null;

            //嵌套类
            protected class Node
            {
                //字段
                public Node next;
                private T data;

                //构造函数
                public Node(T t)
                {
                    next = null;
                    data = t;
                }

                //属性
                public Node Next
                {
                    get
                    {
                        return next;
                    }
                    set
                    {
                        next = value;
                    }
                }
                public T Data
                {
                    get
                    {
                        return data;
                    }
                    set
                    {
                        data = value;
                    }
                }
            }

            //构造函数
            public GenericList()
            {
                head = null;
            }

            //方法1
            public void AddHead(T t)
            {
                Node n = new GenericList<T>.Node(t);
                n.Next = head;
                head = n;
            }

            //方法2
            public System.Collections.Generic.IEnumerator<T> GetEnumerator()
            {
                Node current = head;
                while (current != null)
                {
                    yield return current.Data;
                    current = current.Next;
                }
            }

            //方法3
            // IEnumerable<T> inherits from IEnumerable, therefore this class
            // must implement both the generic and non-generic versions of
            // GetEnumerator. In most cases, the non-generic method can
            // simply call the generic method.
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
        }

        public class SortedList<T> : GenericList<T> where T : System.IComparable <T>
        {
            public void BubbleSort()
            {
                if (null == head || null == head.Next)
                {
                    return;
                }
                bool swapped;

                do
                {
                    Node previous = null;
                    Node current = head;
                    swapped = false;

                    while (current.next != null)
                    {
                        if (current.Data.CompareTo(current.next.Data) > 0)
                        {
                            Node tem = current.next;
                            current.next = current.next.next;
                            tem.next = current;

                            if (previous == null)
                            {
                                head = tem;
                            }
                            else
                            {
                                previous.next = tem;
                            }
                            previous = tem;
                            swapped = true;
                        }
                        else
                        {
                            previous = current;
                            current = current.next;
                        }
                    }
                } while (swapped);
            }
        }

        // A simple class that implements IComparable<T> using itself as the
        // type argument. This is a common design pattern in objects that
        // are stored in generic lists.
        public class Person : System.IComparable<Person>
        {
            string name;
            int age;

            public Person(string s, int i)
            {
                name = s;
                age = i;
            }

            public int CompareTo(Person p)
            {
                return age - p.age;
            }

            public override string ToString()
            {
                return name + ":" + age;
            }

            public bool Equals(Person p)
            {
                return (this.age == p.age);
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                SortedList<Person> List = new SortedList<Person>();

                string[] names = new string[]
                {
                    "Franscoise",
                    "Bill",
                    "Li",
                    "Sandra",
                    "Gunnar",
                    "Alok",
                    "Hiroyuki",
                    "Maria",
                    "Alessandro",
                    "Raul"
                };

                int[] ages = new int[] { 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 };

                for (int x = 0; x < 10; x++)
                {
                    List.AddHead(new Person(names[x], ages[x]));
                }

                foreach (Person p in List)
                {
                    System.Console.WriteLine(p.ToString());
                }
                System.Console.WriteLine("Done with unsorted list");

                List.BubbleSort();
                foreach (Person p in List)
                {
                    System.Console.WriteLine(p.ToString());
                }
                System.Console.WriteLine("Done with sorted list");
                Console.ReadKey();
            }
        }
    }

  • 相关阅读:
    php面试题
    php最基本的缓存之一页面缓存
    原生PHP生成验证码
    原生PHP实现上传大图片与缩略图
    PHP实现连接数据库下载与导入csv格式文件
    ScrollTop火狐谷歌不兼容
    今天学的是 HTML基本元素、基本语法元素特点等,就发图片吧。
    第三天的学习知识HTML5常用的重要单词
    第二天学习了设计方面的知识
    HTLM5第一天的内容
  • 原文地址:https://www.cnblogs.com/cpcpc/p/2123125.html
Copyright © 2020-2023  润新知