单向链表(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 ...
随机推荐
- iwlist等工具的移植
http://blog.csdn.net/jk110333/article/details/8658054 参考了这边文章 -------------------------------------- ...
- STM32F407的硬件I2C
源:STM32F407的硬件I2C 我使用的是STM32的固件库. 硬件模块使用之前必须配置其参数,I2C的配置如下: void IIC_Config(void) { GPIO_InitTypeDef ...
- Openlays 3 绘制基本图形
<body> <div id="menu"> <label>几何图形类型:</label> <select id=" ...
- iOS 程序性能优化
前言 转载自:http://www.samirchen.com/ios-performance-optimization/ 程序性能优化不应该是一件放在功能完成之后的事,对性能的概念应该从我们一开始写 ...
- IE6 下 输入类型表单控件背景问题
.box input{background:url(img/1.jpg) fixed} <body> <div class="box"> <input ...
- 初探JavaScript魅力(四)
选项卡 <title>无标题文档</title> <style> #div1 .active{background:#FF0;} #div1 div{width:2 ...
- 为什么总是要求使用position的时候父类是relative
当我们使用position的时候,一般来说外面的框架是使用relative,里面的元素使用absolute的,这里有两个注意点: 1.如果我们不给父类一个position属性的时候,那么子元素就会以b ...
- PL/SQL Developer StringBuffer 专用复制
;PL/SQL Developer SpecialCopy definition;<LINE_1> for first line;<LINE_*> for all other ...
- Lucene add、updateDocument添加、更新与search查询(转)
package com.lucene; import java.io.IOException; import org.apache.lucene.analysis.standard.Stand ...
- unit正交相机Size的计算公式
如:相机的大小为800*480,要使相机适应800*480像素的图,则 Size = 相机高/2/像素单位 = 480/2/100 = 2.4