C# 基于泛型的自定义线性节点链表集合示例

本例子实现了如何自定义线性节点集合,具体代码如下:

using System;
using System.Collections;
using System.Collections.Generic;

namespace LineNodeDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            LineNodeCollection lineNodes = new LineNodeCollection();
            lineNodes.Add(new LineNode("N1") { Name = "Microsoft" });
            lineNodes.Add(new LineNode("N2") { Name = "Lenovo" });
            lineNodes.Add(new LineNode("N3") { Name = "Apple" });
            lineNodes.Add(new LineNode("N4") { Name = "China Mobile" });
            Console.WriteLine("1、显示全部:");
            lineNodes.ForEach(x => { Console.WriteLine(x.Name); });
            Console.WriteLine("2、删除编号为N2的元素:");
            lineNodes.Remove("N2");
            lineNodes.ForEach(x => { Console.WriteLine(x.Name); });
            Console.WriteLine("3、删除索引为1的元素:");
            lineNodes.RemoveAt(1);
            lineNodes.ForEach(x => { Console.WriteLine(x.Name); });
            Console.WriteLine("4、显示索引为0元素的名称:");
            Console.WriteLine(lineNodes[0].Name);
            Console.WriteLine("5、显示编号为N4元素的名称:");
            Console.WriteLine(lineNodes["N4"].Name);
            Console.WriteLine("6、清空");
            lineNodes.Clear();
            lineNodes.ForEach(x => { Console.WriteLine(x.Name); });
            Console.ReadKey();
        }
    }

    static class Utility
    {
        public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
        {
            foreach(var r in source)
            {
                action(r);
            }
        }
    }

    class LineNodeCollection : IEnumerable<LineNode>
    {
        public IEnumerator<LineNode> GetEnumerator()
        {
            return new LineNodeEnumerator(this.firstLineNode);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        public LineNode this[int index]
        {
            get
            {
                LineNode _lineNode= this.firstLineNode;
                for (int i=0;i<=index;i++)
                {
                    if (_lineNode != null)
                    {
                        if(i<index)
                        {
                            _lineNode = _lineNode.Next;
                            continue;
                        }
                        else
                        {
                            return _lineNode;
                        }
                    }
                    else break;
                }
                throw new IndexOutOfRangeException("超出集合索引范围");
            }
        }

        public LineNode this[string id]
        {
            get
            {
                LineNode _lineNode = this.firstLineNode;
                for (int i = 0; i < this.Count; i++)
                {
                   if(_lineNode.ID == id)
                   {
                        return _lineNode;
                   } 
                   else
                   {
                        _lineNode = _lineNode.Next;
                   }
                }
                throw new IndexOutOfRangeException("未能在集合中找到该元素");
            }
        }

        LineNode firstLineNode;
        LineNode lastLineNode;
        public void Add(LineNode lineNode)
        {
            this.Count++;
            if(this.firstLineNode == null)
            {
                this.firstLineNode = lineNode;
            }
            else
            {
                if (this.firstLineNode.Next == null)
                {
                    lineNode.Previous = this.firstLineNode;
                    this.firstLineNode.Next = lineNode;
                    this.lastLineNode = this.firstLineNode.Next;
                }
                else
                {
                    lineNode.Previous = this.lastLineNode;
                    this.lastLineNode.Next = lineNode;
                    this.lastLineNode = this.lastLineNode.Next;
                }
            }
        }

        public int Count
        {
            private set;get;
        }

        public int IndexOf(string id)
        {
            LineNode _lineNode = this.firstLineNode;
            for (int i = 0; i < this.Count; i++)
            {
                if (_lineNode.ID == id)
                {
                    return i;
                }
                else
                {
                    _lineNode = _lineNode.Next;
                }
            }
            throw new IndexOutOfRangeException("未能在集合中找到该元素");
        }

        public void Clear()
        {
            this.firstLineNode = null;
            this.lastLineNode = null;
            this.Count = 0;
            this.GetEnumerator().Reset();
        }

        public void RemoveAt(int index)
        {
            if (this.Count < index) throw new InvalidOperationException("超出集合索引范围");
            LineNode _currentLineNode = this[index];
            if (_currentLineNode.Previous == null)
            {
                _currentLineNode = _currentLineNode.Next;
                this.firstLineNode = _currentLineNode;
            }
            else 
            {    
                LineNode _lineNode = this.firstLineNode;
                for (int i = 0; i <= index - 1; i++)
                {
                    if (i < index - 1)
                    {
                        _lineNode = _lineNode.Next;
                        continue;
                    }
                    if (i == index - 1)
                    {
                        if (_currentLineNode.Next != null)
                        {
                            _lineNode.Next = _lineNode.Next.Next;
                        }
                        else
                        {
                            this.lastLineNode = _lineNode;
                            _lineNode.Next = null;
                        }
                        break;
                    }
                }
            }
            this.Count--;
        }

        public void Remove(string id)
        {
            int _index = this.IndexOf(id);
            this.RemoveAt(_index);
        }

        public LineNode TopLineNode { get { return this.firstLineNode; } }
        public LineNode LastLineNode { get { return this.lastLineNode; } }
    }

    class LineNodeEnumerator : IEnumerator<LineNode>
    {
        LineNode topLineNode;
        public LineNodeEnumerator(LineNode topLineNode)
        {
            this.topLineNode = topLineNode;
        }
        public LineNode Current
        {
            get
            {
                return this.lineNode;
            }
        }

        object IEnumerator.Current
        {
            get
            {
                return this.Current;
            }
        }

        public void Dispose()
        {
            this.lineNode = null;
        }

        LineNode lineNode;

        public bool MoveNext()
        {
            if(this.lineNode == null)
            {
                this.lineNode = this.topLineNode;
                if (this.lineNode == null) return false;
                if (this.lineNode.Next == null)
                    return false;
                else
                    return true;
            }
            else
            {
                if(this.lineNode.Next !=null)
                {
                    this.lineNode = this.lineNode.Next;
                    return true;
                }
                return false;
            }
        }

        public void Reset()
        {
            this.lineNode = null;
        }
    }


    class LineNode
    {
        public LineNode(string id)
        {
            this.ID = id;
        }
        public string ID { private set; get; }
        public string Name {set; get; }
        public LineNode Next { set; get; }
        public LineNode Previous { set; get; }
    }
}

注意:

①这里所谓的线性节点指定是每个节点之间通过关联前后节点,从而形成链接的节点;

②本示例完全由博主原创,转载请注明来处。

运行结果如下:

示例下载地址:https://pan.baidu.com/s/1eS9UIzS

(请使用VS2015打开,如果为其他版本,请将Program.cs中的内容复制到自己创建的控制台程序中)

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏跟着阿笨一起玩NET

c#实现打印功能

3832
来自专栏一个会写诗的程序员的博客

Spring Reactor 项目核心库Reactor Core

Non-Blocking Reactive Streams Foundation for the JVM both implementing a Reactiv...

2862
来自专栏落花落雨不落叶

canvas画简单电路图

88111
来自专栏我和未来有约会

Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2946
来自专栏陈仁松博客

ASP.NET Core 'Microsoft.Win32.Registry' 错误修复

今天在发布Asp.net Core应用到Azure的时候出现错误InvalidOperationException: Cannot find compilati...

5258
来自专栏菩提树下的杨过

Flash/Flex学习笔记(23):运动学原理

先写一个公用的小球类Ball: package{ import flash.display.Sprite; //小球 类 public class B...

27410
来自专栏闻道于事

js登录滑动验证,不滑动无法登陆

js的判断这里是根据滑块的位置进行判断,应该是用一个flag判断 <%@ page language="java" contentType="text/html...

8868
来自专栏转载gongluck的CSDN博客

cocos2dx 打灰机

#include "GamePlane.h" #include "PlaneSprite.h" #include "BulletNode.h" #include...

7356
来自专栏飞扬的花生

jsencrypt参数前端加密c#解密

      写程序时一般是通过form表单或者ajax方式将参数提交到服务器进行验证,如何防止提交的请求不被抓包后串改,虽然无法说绝对安全却给非法提交提高了难度...

4239
来自专栏芋道源码1024

熔断器 Hystrix 源码解析 —— 断路器 HystrixCircuitBreaker

本文主要基于 Hystrix 1.5.X 版本 1. 概述 2. HystrixCircuitBreaker 3. HystrixCircuitBreaker....

5827

扫码关注云+社区