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 button10_Click(object sender, EventArgs e)
{
    // #1 灵活创建车次信息;选择三个车站 (北京,香河,唐山 )参与计算;

    StationAdvanced bj = stations_advanced[0];
    StationAdvanced xh = stations_advanced[2];
    StationAdvanced ts = stations_advanced[3];

    StringBuilder sb = new StringBuilder();

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

    // 输出车次信息(正向)
    // 北京 出发
    StationAdvanced start = bj;
    while (start != null)
    {
        sb.AppendLine(start.Id + " " + start.Name + "<br>");
        start = start.Next;
    }

    // #3 构造更多车站的车次信息
    // 等于将 香河 插入(插队)
    bj.Next = xh;
    ts.Last = xh;
    xh.Next = ts;
    xh.Last = bj;

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

    // #5 输出车次信息(反向)
    // 唐山 出发
    start = ts;
    sb.AppendLine("车次信息(反向)<br>");
    while (start != null)
    {
        sb.AppendLine(start.Id + " " + start.Name + "<br>");
        start = start.Last;
    }

    webBrowser1.DocumentText = sb.ToString();

}
 

2 代码格式


/// <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 button10_Click(object sender, EventArgs e)
{
    // #1 灵活创建车次信息;选择三个车站 (北京,香河,唐山 )参与计算;

    StationAdvanced bj = stations_advanced[0];
    StationAdvanced xh = stations_advanced[2];
    StationAdvanced ts = stations_advanced[3];

    StringBuilder sb = new StringBuilder();

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

    // 输出车次信息(正向)
    // 北京 出发
    StationAdvanced start = bj;
    while (start != null)
    {
        sb.AppendLine(start.Id + " " + start.Name + "<br>");
        start = start.Next;
    }

    // #3 构造更多车站的车次信息
    // 等于将 香河 插入(插队)
    bj.Next = xh;
    ts.Last = xh;
    xh.Next = ts;
    xh.Last = bj;

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

    // #5 输出车次信息(反向)
    // 唐山 出发
    start = ts;
    sb.AppendLine("车次信息(反向)<br>");
    while (start != null)
    {
        sb.AppendLine(start.Id + " " + start.Name + "<br>");
        start = start.Last;
    }


    webBrowser1.DocumentText = sb.ToString();

}

猜你喜欢

转载自blog.csdn.net/beijinghorn/article/details/132523245