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();
}
}
}

C#_约束 实现可排序单链表的更多相关文章

  1. leetcode题解: Remove Duplicates from Sorted List(已排序单链表去重)

    题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...

  2. Linux 底下使用C语言的 单链表 ,双链表,二叉树 读取文件,并排序

    直接上代码 单链表Linux读文件排序: 双链表Linux读取文件排序: 二叉树LinuX读取文件并排序:

  3. Linux C 单链表 读取文件 并排序 实例并解释

    C的指针挺头疼的,先看一个例子: 给指针赋值和通过指针进行赋值这两种操作的差别确实让人费解.谨记区分的重要方法是:如果对左操作数进行解引用,则修改的是指针所指对象的值:    如果没有使用解引用操作, ...

  4. 148. Sort List (java 给单链表排序)

    题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn) ...

  5. 含头结点的单链表C++实现(包含创建,查找,插入,追加,删除,反转,排序,合并,打印,清空,销毁等基本操作)

    温馨提示:下面代码默认链表数据为字符型,本代码仅供参考,希望能对找到本随笔的人有所帮助! #include<iostream> using namespace std; typedef s ...

  6. Leetcode23--->Merge K sorted Lists(合并k个排序的单链表)

    题目: 合并k个排序将k个已排序的链表合并为一个排好序的链表,并分析其时间复杂度 . 解题思路: 类似于归并排序的思想,lists中存放的是多个单链表,将lists的头和尾两个链表合并,放在头,头向后 ...

  7. Leetcode21--->Merge Two Sorted Lists(合并两个排序的单链表)

    题目: 给出两个排序的单链表,合并两个单链表,返回合并后的结果: 解题思路: 解法还是很简单的,但是需要注意以下几点: 1.  如果两个链表都空,则返回null; 2.  如果链表1空,则返回链表2的 ...

  8. java实现单链表的增删改以及排序

    使用java代码模拟单链表的增删改以及排序功能 代码如下: package com.seizedays.linked_list; public class SingleLinkedListDemo { ...

  9. 史上最全单链表的增删改查反转等操作汇总以及5种排序算法(C语言)

    目录 1.准备工作 2.创建链表 3.打印链表 4.在元素后面插入元素 5.在元素前面增加元素 6.删除链表元素,要注意删除链表尾还是链表头 7.根据传入的数值查询链表 8.修改链表元素 9.求链表长 ...

随机推荐

  1. MATLAB函数表(转自:http://bbs.06climate.com/forum.php?mod=viewthread&tid=16041&extra=page%3D4)

    MATLAB函数表 4.1.1特殊变量与常数 ans 计算结果的变量名 computer 确定运行的计算机 eps 浮点相对精度 Inf 无穷大 I 虚数单位 inputname 输入参数名 NaN ...

  2. 【转】angular通过$http与服务器通信

    http://www.cooklife.cn/detail/54c5044ec93620284e964b58#View angular是一个前端框架,实现了可交互式的页面,但是对于一个web应用,页面 ...

  3. 用JS动态创建登录表单,报了个小错误

    后来发现原来是: dvObj.style.border='#Red 1px sold'; 其中的Red多谢了一个‘#’, 但是奇怪的是在chrome和firefox都备有报错,但是在ie中报错了. 各 ...

  4. DELPHI 中的Delay函数,利用GetTickCount和Application.ProcessMessages构建

      作者 关劲松           delphi 开发中有些时候需要停留片刻,等待界面输入,或异步操作完成,如果使用sleep函数的话,整个程序都会停顿,界面还会出现冻结的情况.因此需要自行编写一个 ...

  5. e8_4输出菲波拉契数列的前10项

    program fbnq;{输出菲波拉契数列的前10项} var a:..] of integer; i:integer; begin a[]:=; a[]:=; do a[i]:=a[i-]+a[i ...

  6. 房租管理小软件(七):flowlayoutPancel 中增加分类控

    见下图的 string FNodeName = dt.Rows[i]["FNodeName"].ToString(); ) { RoomControl.Thumbnail.Grou ...

  7. 多线程之RunLoop

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  8. 给定金额m和红包数量n

    这一题如果是采用暴力手段,不一定能获得正确答案,而且也非常耗时. 所以下面我们采用一个小技巧,也就是先产生n-1个红包,总得sum<m的,这样最后只要添加一个sum-m的红包钱数就可以了. 具体 ...

  9. cocos2d-x编译到android平台后,增加返回键和菜单键支持

    在头文件中增加函数 virtual void keyBackClicked();//android返回键 virtual void keyMenuClicked();//android菜单键 在ini ...

  10. Hbase的安装配置

    一.上传解压后的Hbase文件. 二.配置: 1..bash_profile文件: export HBASE_HOME=/home/kituser/bigdata/hbase-0.94.6-cdh4. ...