C#_约束 实现可排序单链表

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



/*

 使用 约束 实现可排序单链表

 */

namespace UsingConstraints

{

    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 rhs)

        {

            return this.name.CompareTo(rhs.name);

        }

        public bool Equal(Employee rhs)

        {

            return this.name == rhs.name;

        }

    }



    //结点必须实现T的Node的IComparable

    //使用关键字where

    //约束Node只能接受实现了IComparable接口的项

    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(rhs.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;

                }

                //当前结点prev指向新结点

                this.prev = newNode;

                //返回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;

        }



    }



    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;

            }

        }

    }



    class Program

    {

        static void Main(string[] args)

        {

            Program pg = new Program();

            pg.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 nextInt = rand.Next(10);

                Console.Write("{0} ",nextInt);

                myLinkedList.Add(nextInt);

            }

            Console.WriteLine();

            Console.WriteLine("Integer: "+myLinkedList);



            LinkedList<Employee> empLinkedList = new LinkedList<Employee>();

            empLinkedList.Add(new Employee("John"));

            empLinkedList.Add(new Employee("Wang"));

            empLinkedList.Add(new Employee("Lee"));

            //按顺序排序后显示

            Console.WriteLine("class: " + empLinkedList);

            Console.ReadLine();

        }

    }

}


你可能感兴趣的:(单链表)