实现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();
}
}
}
- 实现Icomparable接口(C#)
- IComparable 接口的实现
- 实现System.IComparable接口的一个问题
- IComparable接口和IComparer接口(一)
- 《Effective C#》 Item 26:对需要排序的对象实现IComparable和IComparer接口
- 笔记(显示实现接口,实现IComparable,IComparer接口来排序)
- 实现IComparable
- IComparable 和IComparer 接口实现数组的自定义排列
- Effective C# 使用IComparable和IComparer接口实现排序关系
- 两个对象之间的比较,实现接口IComparable
- c# 实现IComparable、IComparer接口、Comparer类的详解
- IComparable和Icomparer接口
- IComparable接口示例代码
- IComparable和IComparer接口
- C#学习笔记 IComparable接口 IComparable<T>接口 IComparer接口
- C#:IComparable和ICompare
- Icomparable与Icomparer实现显示接口以显示接口排序为准
- [泛型]-泛型接口IComparable
- TCP 和 UDP 协议发送数据包的大小(浅析)
- HTML 标签
- Additional Named Entities for HTML HTML附加命名实体
- Spring框架快速入门之简介
- 客户端用https连接服务器的一点心得
- 实现Icomparable接口(C#)
- 关于批处理
- 反转一个字节和判断32位整数二进制中1的个数等算法
- 如果你能看到,请你帮我一起转,转到全中国
- linux下cvs配置
- 写给金融危机下年轻人的16条忠告(zt)
- FFMPEG笔记1
- ASP.NETDataGrid应用收集
- Python 3.0 正式版发布