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();
}
}
}
C#_约束 实现可排序单链表的更多相关文章
- leetcode题解: Remove Duplicates from Sorted List(已排序单链表去重)
题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...
- Linux 底下使用C语言的 单链表 ,双链表,二叉树 读取文件,并排序
直接上代码 单链表Linux读文件排序: 双链表Linux读取文件排序: 二叉树LinuX读取文件并排序:
- Linux C 单链表 读取文件 并排序 实例并解释
C的指针挺头疼的,先看一个例子: 给指针赋值和通过指针进行赋值这两种操作的差别确实让人费解.谨记区分的重要方法是:如果对左操作数进行解引用,则修改的是指针所指对象的值: 如果没有使用解引用操作, ...
- 148. Sort List (java 给单链表排序)
题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn) ...
- 含头结点的单链表C++实现(包含创建,查找,插入,追加,删除,反转,排序,合并,打印,清空,销毁等基本操作)
温馨提示:下面代码默认链表数据为字符型,本代码仅供参考,希望能对找到本随笔的人有所帮助! #include<iostream> using namespace std; typedef s ...
- Leetcode23--->Merge K sorted Lists(合并k个排序的单链表)
题目: 合并k个排序将k个已排序的链表合并为一个排好序的链表,并分析其时间复杂度 . 解题思路: 类似于归并排序的思想,lists中存放的是多个单链表,将lists的头和尾两个链表合并,放在头,头向后 ...
- Leetcode21--->Merge Two Sorted Lists(合并两个排序的单链表)
题目: 给出两个排序的单链表,合并两个单链表,返回合并后的结果: 解题思路: 解法还是很简单的,但是需要注意以下几点: 1. 如果两个链表都空,则返回null; 2. 如果链表1空,则返回链表2的 ...
- java实现单链表的增删改以及排序
使用java代码模拟单链表的增删改以及排序功能 代码如下: package com.seizedays.linked_list; public class SingleLinkedListDemo { ...
- 史上最全单链表的增删改查反转等操作汇总以及5种排序算法(C语言)
目录 1.准备工作 2.创建链表 3.打印链表 4.在元素后面插入元素 5.在元素前面增加元素 6.删除链表元素,要注意删除链表尾还是链表头 7.根据传入的数值查询链表 8.修改链表元素 9.求链表长 ...
随机推荐
- Spring编程风格
给自己使用的无需定义接口:即一个模块内部的都是封装的,定义接口并不会得到很多好处,变过几次实现?? “优先面向接口编程,而非实现” 不是必须,是优先: 给朋友(第三方)使用的定义接口:即要公开的功能, ...
- Mac: the original hosts
## # Host Database # # localhost is used to configure the loopback interface # when the system is bo ...
- 【转】Application.mk 文件语法规范
原文网址:http://blog.sina.com.cn/s/blog_4c451e0e0100s6q4.html Application.mk file syntax specification A ...
- 如何获取android app的Activity
方法一 如有你有待测项目的源码,那么直接查看源码就好.如果没有,那么请联系有源码的同学,这是推荐方法. 方法二 直接把apk后缀改为zip格式,打开压缩包后再打开AndroidManifest.x ...
- [LeetCode]Linked List Cycle II解法学习
问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...
- bzoj 1432 [ZJOI2009]Function(找规律)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1432 [思路] 找(cha)规(ti)律(jie) 分析戳这儿 click here ...
- leetcode@ [136/137] Single Number & Single Number II
https://leetcode.com/problems/single-number/ Given an array of integers, every element appears twice ...
- CDH CM安装及简单群集部署测试
前吃人的故事开始了,金钱是如何吃人的呢?我在想ing,还没想通,一起吧,哈哈: 入题,别胡扯,误人子弟!!!! CM@@!!!!!!....................., 先来张monitor ...
- Django – vicalloy's trac
Django – vicalloy's trac Search:
- daemon not running. starting it now on port 5037 ADB server didn't ACK
adb kill-server adb start-server 显示如下 daemon not running. starting it now on port 5037 ADB server di ...