C#,《小白学程序》第十一课:双向链表(Linked-List)其二,链表的插入与删除的方法(函数)与代码

1 文本格式

/// <summary>

/// 改进的车站信息类 class

/// 增加了 链表 需要的两个属性 Last Next

/// </summary>

public class StationAdvanced

{

/// <summary>

/// 编号

/// </summary>

public int Id { get; set; } = 0;

/// <summary>

/// 车站名

/// </summary>

public string Name { get; set; } = string.Empty;

public StationAdvanced Last { get; set; } = null;

public StationAdvanced Next { get; set; } = null;

public StationAdvanced(int id, string name)

{

this.Id = id;

this.Name = name;

}

}

// 列表的初值

List<StationAdvanced> stations_advanced = new List<StationAdvanced>() {

new StationAdvanced(1,"北京"),

new StationAdvanced(2,"石家庄"),

new StationAdvanced(3,"香河"),

new StationAdvanced(4,"唐山"),

new StationAdvanced(5,"北戴河"),

new StationAdvanced(6,"秦皇岛"),

new StationAdvanced(7,"廊坊"),

new StationAdvanced(8,"天津"),

};

/// <summary>

/// 《小白学程序》第十三课:双向链表(Linked-List)第二,链表插入与删除的计算方法与代码

/// 本课是《小白学程序》第十课之续章。

/// 学习用 函数 来实现 链表的(节点)插入、删除,而不是手工输入。

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button13_Click(object sender, EventArgs e)

{

StationAdvanced bj = stations_advanced[0];

StationAdvanced xh = stations_advanced[2];

StationAdvanced ts = stations_advanced[3];

StationAdvanced qhd = stations_advanced[5];

// #2 构造车次信息(直达)

// 设置 北京 下一站为 秦皇岛

// 设置 秦皇岛唐山 上一站为 北京

bj.Next = qhd;

qhd.Last = bj;

// 调用链表的 插入 删除 算法

// 插入香河

StationInsert(bj, xh);

// 插入唐山

StationInsert(xh, ts);

// #4 输出车次信息(正向)

StringBuilder sb = new StringBuilder();

sb.AppendLine("插入新车站:<br>");

// 北京 出发

StationAdvanced start = bj;

while (start != null)

{

sb.AppendLine(start.Id + " " + start.Name + "<br>");

start = start.Next;

}

sb.AppendLine("<br>");

// 删除 香河

StationRemove(xh);

// #4 输出车次信息(正向)

// 北京 出发

sb.AppendLine("删除指定车站:<br>");

start = bj;

while (start != null)

{

sb.AppendLine(start.Id + " " + start.Name + "<br>");

start = start.Next;

}

webBrowser1.DocumentText = sb.ToString();

}

/// <summary>

/// 双向链表的插入算法

/// 将 c 节点插入 a (之后) b 之间

/// </summary>

/// <param name="a"></param>

/// <param name="c"></param>

private void StationInsert(StationAdvanced a, StationAdvanced c)

{

StationAdvanced b = a.Next;

a.Next = c;

c.Last = a;

c.Next = b;

b.Last = c;

}

/// <summary>

/// 双向链表的删除算法

/// 删除 a (之后) 的 b 节点

/// </summary>

/// <param name="a"></param>

private void StationRemove(StationAdvanced b)

{

StationAdvanced a = b.Last;

StationAdvanced c = b.Next;

a.Next = c;

c.Last = a;

}

2 代码格式

cs 复制代码
/// <summary>
/// 改进的车站信息类 class
/// 增加了 链表 需要的两个属性 Last Next
/// </summary>
public class StationAdvanced
{
    /// <summary>
    /// 编号
    /// </summary>
    public int Id { get; set; } = 0;
    /// <summary>
    /// 车站名
    /// </summary>
    public string Name { get; set; } = string.Empty;
    public StationAdvanced Last { get; set; } = null;
    public StationAdvanced Next { get; set; } = null;

    public StationAdvanced(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
}

// 列表的初值
List<StationAdvanced> stations_advanced = new List<StationAdvanced>() {
        new StationAdvanced(1,"北京"),
        new StationAdvanced(2,"石家庄"),
        new StationAdvanced(3,"香河"),
        new StationAdvanced(4,"唐山"),
        new StationAdvanced(5,"北戴河"),
        new StationAdvanced(6,"秦皇岛"),
        new StationAdvanced(7,"廊坊"),
        new StationAdvanced(8,"天津"),
};

/// <summary>
/// 《小白学程序》第十三课:双向链表(Linked-List)第二,链表插入与删除的计算方法与代码
/// 本课是《小白学程序》第十课之续章。
/// 学习用 函数 来实现 链表的(节点)插入、删除,而不是手工输入。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button13_Click(object sender, EventArgs e)
{
    StationAdvanced bj = stations_advanced[0];
    StationAdvanced xh = stations_advanced[2];
    StationAdvanced ts = stations_advanced[3];
    StationAdvanced qhd = stations_advanced[5];

    // #2 构造车次信息(直达)
    // 设置 北京 下一站为 秦皇岛
    // 设置 秦皇岛唐山 上一站为 北京 
    bj.Next = qhd;
    qhd.Last = bj;

    // 调用链表的 插入 删除 算法
    // 插入香河
    StationInsert(bj, xh);
    // 插入唐山
    StationInsert(xh, ts);

    // #4 输出车次信息(正向)
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("插入新车站:<br>");
    // 北京 出发
    StationAdvanced start = bj;
    while (start != null)
    {
        sb.AppendLine(start.Id + " " + start.Name + "<br>");
        start = start.Next;
    }
    sb.AppendLine("<br>");

    // 删除 香河
    StationRemove(xh);

    // #4 输出车次信息(正向)
    // 北京 出发
    sb.AppendLine("删除指定车站:<br>");
    start = bj;
    while (start != null)
    {
        sb.AppendLine(start.Id + " " + start.Name + "<br>");
        start = start.Next;
    }

    webBrowser1.DocumentText = sb.ToString();
}

/// <summary>
/// 双向链表的插入算法
/// 将 c 节点插入 a (之后) b 之间
/// </summary>
/// <param name="a"></param>
/// <param name="c"></param>
private void StationInsert(StationAdvanced a, StationAdvanced c)
{
    StationAdvanced b = a.Next;
    a.Next = c;
    c.Last = a;
    c.Next = b;
    b.Last = c;
}

/// <summary>
/// 双向链表的删除算法
/// 删除 a (之后) 的 b 节点
/// </summary>
/// <param name="a"></param>
private void StationRemove(StationAdvanced b)
{
    StationAdvanced a = b.Last;
    StationAdvanced c = b.Next;
    a.Next = c;
    c.Last = a;
}

3 计算结果

相关推荐
luckys.one1 小时前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
ccut 第一混2 小时前
c# 调用basler 相机
c#·halcon·basler
TomCode先生2 小时前
c#动态树形表达式详解
开发语言·c#
高-老师2 小时前
基于R语言的物种气候生态位动态量化与分布特征模拟
开发语言·r语言·物种气候
大翻哥哥3 小时前
Python 2025:量化金融与智能交易的新纪元
开发语言·python·金融
~|Bernard|3 小时前
在 PyCharm 里怎么“点鼠标”完成指令同样的运行操作
算法·conda
战术摸鱼大师3 小时前
电机控制(四)-级联PID控制器与参数整定(MATLAB&Simulink)
算法·matlab·运动控制·电机控制
Christo33 小时前
TFS-2018《On the convergence of the sparse possibilistic c-means algorithm》
人工智能·算法·机器学习·数据挖掘
weixin_437830943 小时前
使用冰狐智能辅助实现图形列表自动点击:OCR与HID技术详解
开发语言·javascript·ocr
鹿鹿学长4 小时前
2025年全国大学生数学建模竞赛(C题) 建模解析|婴儿染色体数学建模|小鹿学长带队指引全代码文章与思路
c语言·开发语言·数学建模