单向链表(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 ...
随机推荐
- winxp sp2
不支持sha2,所以ssl建立失败, sp3就可以了 http://superuser.com/questions/802693/sha2-support-for-windows-xp-sp2-any ...
- FreeRTOS中断优先级配置(重要)
FreeRTOS中断优先级配置(重要) 本章节为大家讲解FreeRTOS中断优先级配置,此章节非常重要,初学者经常在这里犯迷糊.对于初学者来说,本章节务必要整明白.12.1 NVIC基础知识12.2 ...
- Html的第一次小结
一 Html的文档结构 (1) <html> 标记html文件的头标记,没有什么实质性的作用,但是却是必不可少的 (2) <head> 放置html文件信息.如css的一些 ...
- NSTimer内存方面的探究
今天研究一个框架,看到它对NSTimer的处理,感觉很有意思.于是自己在各种情况下都研究了一下,现总结如下. 我们用到NSTimer时,似乎习惯于会在dealloc方法中把它invalidate掉,但 ...
- XMEAG-128A1
JTAGUSERID = 0x00 WDWP = 8CLK WDP = 8CLK DVSDON = [ ] BOOTRST = BOOTLDR BODPD = DISABLED RSTDISBL = ...
- TcpClient 读写流
TcpClient 读写流 TcpClient tcp = new TcpClient(); tcp.Connect(IPAddress.Parse("192.168.1.161" ...
- hdu1501 Zipper
Zipper Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- TM3、4波段GeoTiff数据计算NDVI
源码: 1: PRO TIFF_NDVI,F1,F2,FOUT 2: F1 = DIALOG_PICKFILE(TITLE = 'B4 TIFF',FILTER='*.TIF',/READ) 3: ...
- Codeforces 691A Fashion in Berland
水题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #includ ...
- 2333: [SCOI2011]棘手的操作[写不出来]
2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1979 Solved: 772[Submit][Stat ...