C#自定义链表
来源:互联网 发布:外汇交易员的一天知乎 编辑:程序博客网 时间:2024/05/21 06:37
C#自定义链表,测试图:
下面是代码:
// 自写,如有错误请见谅。// 自定义链表,包括索引器,添加及删除功能。// 仅仅作为mark~using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication16{ class Program { static void Main(string[] args) { LinkedListTest<int> test = new LinkedListTest<int> { }; test.Add(0); test.Add(1); test.Add(2); test.Add(3); Console.WriteLine("链表添加元素测试:"); for(int i = 0; i< test.Length();i++) Console.WriteLine("test{0}", i + " = " + test[i]); Console.WriteLine("为链表中2位置添加数字5测试:"); test.AddAt(2,5); for (int i = 0; i < test.Length(); i++) Console.WriteLine("test{0}", i + " = " + test[i]); Console.WriteLine("删除链表中2位置测试:"); test.RemoveAt(2); for (int i = 0; i < test.Length(); i++) Console.WriteLine("test{0}", i + " = " + test[i]); Console.ReadKey(); } } class LinkedListTest <T> { LinkedListTest<T> frontNode; public T val; LinkedListTest<T> nextNode; // 在链表末尾添加元素 public void Add (T v) { LinkedListTest<T> node = this; while (node.nextNode != null) { node = node.nextNode; } node.nextNode = new LinkedListTest<T> { val = v }; node.nextNode.frontNode = node; } // 在链表的某个位置添加元素 public void AddAt(int index,T value) { LinkedListTest<T> node = this; for(int i = 0; i < index + 1; i++) { node = node.nextNode; } node.frontNode.nextNode = new LinkedListTest<T> { val = value }; node.frontNode.nextNode.frontNode = node.frontNode; node.frontNode.nextNode.nextNode = node; node.frontNode = node.frontNode.nextNode; } // 在链表某个位置删除元素 public void RemoveAt(int index) { LinkedListTest<T> node = this; for (int i = 0; i < index + 1; i++) { node = node.nextNode; } node.frontNode.nextNode = node.nextNode; node.nextNode.frontNode = node.frontNode; node = null; } //链表总长度 public int Length() { int count = 0; LinkedListTest<T> node = this; while(node.nextNode != null) { node = node.nextNode; count++; } return count; } // 链表索引器 public T this[int index] { get { LinkedListTest<T> node = this; for(int i = 0;i <= index;i++) { node = node.nextNode; } return node.val; } } }}
0 0
- C#自定义链表
- C#自定义链表
- C# 双向链表及自定义链表
- C#自定义双向链表,功能类似C++中的CList
- C# 自定义sqlserver表值函数
- C#调用自定义表类型参数
- C# 自定义控件,自定义属性,自定义事件
- c#自定义简单链表通过IEnumerable接口实现内部迭代
- c#自定义简单链表通过IEnumerable接口,范型实现内部迭代
- C#自定义Attribute举例!
- 自定义显示图片--c#
- C# 自定义异常类
- C# 自定义EVENT
- C#中的自定义消息
- C#中自定义异常
- c#自定义属性
- C# 自定义属性
- C# 自定义用户控件
- hibernate 抛出could not fetch initial value for increment generator
- nodejs npm install全局安装和本地安装的区别
- javax.mail 处理邮件时由于content-type内容不合标准引起的错误
- 博客简介
- C#语言写微信小程序之visual studio+微信开发者工具写一个简单的微信小程序
- C#自定义链表
- 多表查询
- ubuntu 安装配置 Jenkins
- HTML5验证表单内容是否为空
- MongoDB 的简单使用(更新)
- 关于ie下js的chilren和childrenNode
- java和 javaw 以及 javaws的区别
- 系统时间以及变量的修改,last,lsatlog,tty,wall write命令常用用法
- HTML5验证邮政地址是否合法