单向链表(C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Collections; namespace ConsoleApplication2
{
public class Program
{
public static void Main()
{
SinglyLinked<int> noteLink = new SinglyLinked<int>(); Console.WriteLine("新增数据");
noteLink.AddNote();
noteLink.AddNote();
noteLink.AddNote();
noteLink.AddNote();
noteLink.AddNote();
foreach (var item in noteLink)
{
Console.WriteLine(item);
} Console.WriteLine("删除数据2");
noteLink.RemoveNote();
foreach (var item in noteLink)
{
Console.WriteLine(item);
} Console.WriteLine("在8前面增加99");
noteLink.InsertItemBefore(,); foreach (var item in noteLink)
{
Console.WriteLine(item);
} Console.WriteLine("在1后面增加55");
noteLink.InsertItemAfter(,);
foreach (var item in noteLink)
{
Console.WriteLine(item);
} Console.WriteLine("节点数量:"+noteLink.Count); Console.Read();
}
} public class SinglyLinked<T> where T : IComparable
{
public DNote<T> HeadNote; public int Count{get;set;} public SinglyLinked()
{
HeadNote = new DNote<T>(default(T));
} public void AddNote(T t)
{
DNote<T> tNote = HeadNote;
while (tNote.NextNote!= null)
{
tNote = tNote.NextNote;
} tNote.NextNote = new DNote<T>(t); Count++;
} public IEnumerator<T> GetEnumerator()
{
DNote<T> tNote = HeadNote;
while (tNote.NextNote != null)
{
tNote = tNote.NextNote;
yield return tNote.NoteValue;
}
} public void RemoveNote(T t)
{
DNote<T> tNote = HeadNote;
do
{
if (tNote.NextNote.NoteValue.CompareTo(t) == )
{
tNote.NextNote = tNote.NextNote.NextNote;
Count--;
}
tNote = tNote.NextNote;
} while (tNote!=null && tNote.NextNote!=null);
} public void InsertItemBefore(T t,T insertNoteValue)
{
DNote<T> tNote = HeadNote;
do
{
if (tNote.NextNote.NoteValue.CompareTo(t) == )
{
var nextNote = tNote.NextNote;
var newNote = new DNote<T>(insertNoteValue);
newNote.NextNote = nextNote;
tNote.NextNote = newNote;
Count++;
break;
}
tNote = tNote.NextNote;
} while (tNote != null && tNote.NextNote != null);
} public void InsertItemAfter(T t, T insertNoteValue)
{
DNote<T> tNote = HeadNote;
do
{
if (tNote.NextNote.NoteValue.CompareTo(t) == )
{
var nextNote = tNote.NextNote.NextNote;
var newNote = new DNote<T>(insertNoteValue);
newNote.NextNote = nextNote;
tNote.NextNote.NextNote = newNote;
Count++;
break;
}
tNote = tNote.NextNote;
} while (tNote != null && tNote.NextNote != null);
}
} public class DNote<T>
{
public DNote<T> NextNote { get; set;} public T NoteValue { get; set; } public DNote(T t)
{
NoteValue = t;
}
} }
单向链表(C#)的更多相关文章
- Reverse Linked List II 单向链表逆序(部分逆序)
0 问题描述 原题点击这里. 将单向链表第m个位置到第n个位置倒序连接.例如, 原链表:1->2->3->4->5, m=2, n =4 新链表:1->4->3-& ...
- 【编程题目】输入一个单向链表,输出该链表中倒数第 k 个结点
第 13 题(链表):题目:输入一个单向链表,输出该链表中倒数第 k 个结点.链表的倒数第 0 个结点为链表的尾指针.链表结点定义如下: struct ListNode {int m_nKey;Lis ...
- 输出单向链表中倒数第k个结点
描述 输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第0个结点为链表的尾指针. 链表结点定义如下: struct ListNode { int m_nKey; ListNode* ...
- Linus:利用二级指针删除单向链表
Linus大神在slashdot上回答一些编程爱好者的提问,其中一个人问他什么样的代码是他所喜好的,大婶表述了自己一些观点之后,举了一个指针的例子,解释了什么才是core low-level codi ...
- 【转】Linus:利用二级指针删除单向链表
原文作者:陈皓 原文链接:http://coolshell.cn/articles/8990.html 感谢网友full_of_bull投递此文(注:此文最初发表在这个这里,我对原文后半段修改了许多, ...
- C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)
#include<stdio.h> #include<malloc.h> #define LEN sizeof(struct Student) struct Student / ...
- 数据结构——Java实现单向链表
结点类: /** * @author zhengbinMac * 一个OnelinkNode类的对象只表示链表中的一个结点,通过成员变量next的自引用方式实现线性表中各数据元素的逻辑关系. */ p ...
- 输入一个单向链表,输出该链表中倒数第K个结点
输入一个单向链表,输出该链表中倒数第K个结点,具体实现如下: #include <iostream> using namespace std; struct LinkNode { publ ...
- 单向链表JAVA代码
//单向链表类 publicclassLinkList{ //结点类 publicclassNode{ publicObject data; ...
- C++ 单向链表反转
单向链表反转,一道常见的面试题,动手实现下. #include "stdafx.h" #include <stdlib.h> struct Node{ int data ...
随机推荐
- http get with body data
http://stackoverflow.com/questions/978061/http-get-with-request-body Yes, you can send a request bod ...
- .project
http://blog.csdn.net/qiushuichangtian888/article/details/9299843 一个老项目导入新环境后老是提示build.properties不存在的 ...
- WPF 常用样式
TextBox <Window x:Class="WpfDemo.ListBoxTemaple" xmlns="http://schemas.microsoft.c ...
- Hadoop RPC机制
RPC(Remote Procedure Call Protocol)远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.Hadoop底层的交互都是通过 rp ...
- mplayer最全的命令
前段时间做过qt内嵌mplayer的一个小程序,感觉mplayer还行不过不支持打开图片感觉有点无力.话不多说上代码: QString path="d:/1.mkv"; QWidg ...
- 前端知识复习一(css)
1.清楚浮动 父盒子高度为0,子盒子全部定位.浮动.子盒子不会撑开父盒子,下面的盒子会顶上来 清楚方法: clear:both: overflow:hidden: 加空标签 单/双 //双标签 .cl ...
- Android开发之如何保证Service不被杀掉(broadcast+system/app)
序言 最近项目要实现这样一个效果:运行后,要有一个service始终保持在后台运行,不管用户作出什么操作,都要保证service不被kill,这可真是一个难题.参考了现今各种定制版的系统和安全厂商牛虻 ...
- 服务器修改 ssh 22端口
我们首先修改配置文件,让ssh 通知支持2个端口访问 ,22 和 23456 端口. Port 22 Port 23456 为什么要这样: 万一我用 23456端口替换了22端口,但是 23456正好 ...
- 把MSSQL的表数据查询成 insert into格式的函数
USE [db] GO /****** Object: StoredProcedure [dbo].[proc_insert] Script Date: 12/05/2012 17:18:31 *** ...
- QWidget QMainWindow QDialog 三个基类的区别
Qt类是一个提供所需的像全局变量一样的大量不同的标识符的命名空间.通常情况下,你可以忽略这个类.QObject和一些其它类继承了它,所以在这个Qt命名空间中定义的所有标识符通常情况下都可以无限制的使用 ...