实现Icomparable接口(C#)

来源:互联网 发布:网页三剑客软件 编辑:程序博客网 时间:2024/05/22 13:22

 实现Icomparable接口(C#)
下面将实现一个简单的可排序的泛型链表,其结点必须实现IComparer接口。也就是满足这样的表达式:
public class Node<T> :
    IComparable<Node<T>> where T : IComparable<T>

具体代码:
using System;
using System.Collections.Generic;

namespace MyGenerics
{
    //实现了接口的类,供测试
    public class Employee : IComparable<Employee>
    {
        private string name;
        public Employee(string name)
        {
            this.name = name;
        }

        public override string ToString()
        {
            return this.name;
        }
        //实现方法
        public int CompareTo(Employee other)
        {
            return this.name.CompareTo(other.name);
        }
        public bool Equals(Employee rhs)
        {
            return this.name == rhs.name;
        }
    }
    //结点
    public class Node<T> : IComparable<Node<T>> where T : IComparable<T>
    {
        private T data;
        private Node<T> next = null;
        private Node<T> prev = null;

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

        public T Data
        {
            get { return this.data; }
        }
        public Node<T> Next
        {
            get { return this.next; }
        }
        public int CompareTo(Node<T> rhs)
        {
            return data.CompareTo(rhs.data);
        }
        public bool Equals(Node<T> rhs)
        {
            return this.data.Equals(this.data);
        }

        //增加一个结点
        public Node<T> Add(Node<T> newNode)
        {
            if (this.CompareTo(newNode) > 0)
            {
                newNode.next = this;
                if (this.prev != null)
                {
                    this.prev.next = newNode;
                    newNode.prev = this.prev;
                }
                this.prev = newNode;

                return newNode;
            }
            else
            {
                if (this.next != null)
                {
                    this.next.Add(newNode);
                }
                else
                {
                    this.next = newNode;
                    newNode.prev = this;
                }

                return this;
            }
        }
        public override string ToString()
        {
            string output = data.ToString();
            if (next != null)
            {
                output += "," + next.ToString();
            }
            return output;
        }
    }
    //链表
    public class LinkedList<T> where T : IComparable<T>
    {
        private Node<T> headNode = null;

        public T this[int index]
        {
            get
            {
                int ctr = 0;
                Node<T> node = headNode;

                while (node != null && ctr <= index)
                {
                    if (ctr == index)
                    {
                        return node.Data;
                    }
                    else
                    {
                        node = node.Next;
                    }
                    ++ctr;
                }
                throw new ArgumentOutOfRangeException();
            }
        }
        public LinkedList()
        { }
        public void Add(T data)
        {
            if (headNode == null)
            {
                headNode = new Node<T>(data);
            }
            else
            {
                headNode = headNode.Add(new Node<T>(data));
            }
        }
        public override string ToString()
        {
            if (this.headNode != null)
            {
                return this.headNode.ToString();
            }
            else
            {
                return string.Empty;
            }
        }
    }
    public class TestConstraints
    {
        //主程序入口
        static void Main(string[] args)
        {
            TestConstraints t = new TestConstraints();
            t.Run();
        }
        public void Run()
        {
            LinkedList<int> myLinkedList = new LinkedList<int>();
            Random rand = new Random();
            Console.Write("Adding: ");
            for (int i = 0; i < 10; i++)
            {
                int next = rand.Next(10);
                Console.Write("{0} ", next);
                myLinkedList.Add(next);
            }
            LinkedList<Employee> em = new LinkedList<Employee>();
            em.Add(new Employee("john"));
            em.Add(new Employee("Paul"));
            em.Add(new Employee("george"));
            em.Add(new Employee("Ringo"));
            em.Add(new Employee("lanmao100"));

            Console.WriteLine("/nRetrieving collections...");
            Console.WriteLine("Integers: " + myLinkedList);
            Console.WriteLine("Employees:" + em);
            Console.ReadLine();
        }
    }
}

原创粉丝点击