zoukankan      html  css  js  c++  java
  • 学习笔记链表练习,模仿StringBuilder的山寨版StringBuilder

    学习笔记---链表练习,模仿StringBuilder的山寨版StringBuilder,见代码

    代码
    using System;

    namespace System.MyText//山寨命名空间
    {
    //表示链表,内嵌节点类的定义
    public sealed class NodeBuilder
    {
    //定义节点类,嵌套类,对内可用,对外隐藏
    sealed class Node
    {
    private char _nodevalue;//节点值
    private Node _nextnode;//下一节点

    //public Node(char nodevalue,Node nextnode)//经典错误,产生了“鸡与蛋的问题”
    public Node(char nodevalue)
    {
    this._nodevalue = nodevalue;
    this._nextnode = null;
    }

    //节点通常要求:内部值不可改变,称为可换不可修
    public char NodeValue
    {
    get
    {
    return this._nodevalue;
    }
    }
    //本处为了单纯的拼合操作要求,只有空节点之后方能添加新的有效节点
    public Node NextNode
    {
    get
    {
    return this._nextnode;
    }
    set
    {
    if (value != null)//传入的节点有效
    {
    if (this.NextNode == null)//本节点后续为空
    {
    this._nextnode = value;//将新的节点接续在本节点之后
    }
    else
    {
    //nothing to do here!
    }
    }
    else
    {
    //nothing to do here!
    }
    }
    }
    }

    //链表内部成员
    private Node _headnode;//头节点
    private Node _endnode;//尾节点
    private int _nodecount;//节点计数

    //无参构造---造成一个有效的空链表---有容器,但是无数据内容
    public NodeBuilder()
    {
    this._headnode = null;
    this._endnode = null;
    this._nodecount = 0;
    }

    //唯一可以直接对外公开的属性
    public int NodeCount
    {
    get
    {
    return this._nodecount;
    }
    }

    //添加字符串的对外方法
    public void Append(string text)
    {
    if (text != null && text.Length > 0)
    {
    Node newnode
    = null;
    foreach (char onechar in text)
    {
    newnode
    = new Node(onechar);
    this.addNode(newnode);
    }
    }
    else
    {
    //nothing to do here!
    }
    }
    //私有业务方法---直接添加节点
    private void addNode(Node node)
    {
    if (this._headnode == null)//当前为空表
    {
    this._headnode = node;
    this._endnode = node;
    }
    else//当前不为空表
    {
    this._endnode.NextNode = node;//新节点接续在尾节点之后
    this._endnode = this._endnode.NextNode;//当前尾节点向后移动一位
    }
    this._nodecount++;
    }

    //将链表内容转化为字符串的公有方法
    public override string ToString()
    {
    string str;

    //字符串---字符串成串---字符集合---字符数组
    char[] ary = new char[this.NodeCount];
    //填充字符数组
    Node currentnode;//表示当前节点的访问权,类似于数据库中的游标
    currentnode = this._headnode;//将访问权重置在集合头部
    int index = 0;
    while (currentnode != null)
    {
    ary[index]
    = currentnode.NodeValue;//填充当前节点的字符值
    currentnode = currentnode.NextNode;//将当前操作位置后移一位
    index++;//将数组中的当前操作位置对应后移一位
    }
    str
    = new String(ary);

    return str;
    }
    }
    }
  • 相关阅读:
    Android程序反编译的方法(转)
    VMware的Easy Install安装
    Oracle ORA12514:TNS:监听程序当前无法识别连接描述符中请求的服务
    Java Eclipse 如何导入外部Jar包
    Oracle IMP00010: 不是有效的导出文件,标题验证失败
    怎样取消Windows 2003 server 意外关机提示
    没有文件扩展“.vbs”的脚本引擎的解决方案
    Oracle init.ora常用配置详解
    eclipse断点调试 出现Source not found
    VMware Workstation 8
  • 原文地址:https://www.cnblogs.com/cs_net/p/1837969.html
Copyright © 2011-2022 走看看