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. ORACLE SQLloader详细语法

    Oracle   SQL   Loader的详细语法     SQL*LOADER是ORACLE的数据加载工具,通常用来将操作系统文件迁移到ORACLE数据库中.SQL*LOADER是大型数据     ...

  2. (转载)HTTP URL

    HTTP URL的格式如下: http://host[“:”post][abs_path] 其中http表示要通过HTTP协议来定位网络资源.host表示合法的Internet主机域名或IP地址(以点 ...

  3. CF GYM 100703B Energy Saving

    题意:王子每月买m个灯泡给n个房间换灯泡,如果当前有的灯泡数够列表的第一个房间换的就全换,直到灯泡不够为止,给出q个查询,查询x月已经换好几个房子,手里还剩多少灯泡. 解法:水题……小模拟. 代码: ...

  4. HDU5692 Snacks DFS+线段树

    分析:一棵以1为根的有根树,然后每个点维护从根到当前节点的路径和,当修改一个点时 只会影响的子树的和,最优值也是子树最大的值 #include <cstdio> #include < ...

  5. HDU1540 Tunnel Warfare 水题

    分析:不需要线段树,set可过,STL大法好 #include <iostream> #include <cstdio> #include <cstring> #i ...

  6. selenium Grid

    Selenium Grid 的机制是启动一个 hub,然后启动多个 Selenium RC 注册到 hub 上, 当测试请求到 hub 时,hub 会将测试分发给 Selenium RC, Selen ...

  7. Jquery+bootstrap实现静态博客主题

    来源:个人博客     body部分:   side部分:     文章页面: 下载链接:             Fork Git: https://github.com/dwqs/theme1   ...

  8. Android实例-操作摄像头(XE8+小米2)

    结果: 1.同样是照相,自己的程序设置为高质量时刷新慢,而小米手机的相机那真心反映快呀. 2.就算我设置为最高质量,可相片也没有小米手机的相片大.我最大是2000*1000,而小米可以做到3000*2 ...

  9. delphi -- 进制转换 函数表

    1.16 TO 10 ******************************************************** 16转10,否则输出-1 function Hex(c: cha ...

  10. 100% opacity UILabel over a 50% opacity background (UIView?) UIView是百分之50透明而上面的UILable是100%不透明

    So right now I have a UIView with a UILabel in it. I want the background to have an opacity < 1.0 ...