栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#如何自定义线性节点链表集合

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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(this IEnumerable source, Action action)
  {
   foreach(var r in source)
   {
    action(r);
   }
  }
 }

 class LineNodeCollection : IEnumerable
 {
  public IEnumerator 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
 {
  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; }
 }
}

注意:

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

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

 运行结果如下:

示例下载地址:C#自定义线性节点链表集合

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123309.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号