C#, "Xiaobai Learning Program" Lesson 10: Two-way list (Linked-List) one, use linked list to create high-speed rail train information (native algorithm)

1 text format


/// <summary>
/// Improved station information class
/// Added two attributes Last Next
/// </summary>
public class StationAdvanced
{     /// <summary>     /// number     / // </summary>     public int Id { get; set; } = 0;     /// <summary>     /// station name     /// </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;
    }
}

// The initial value of the list
List<StationAdvanced> stations_advanced = new List<StationAdvanced>() {         new StationAdvanced(1,"Beijing"),         new StationAdvanced(2,"Shijiazhuang"),         new StationAdvanced(3,"Xianghe"),         new StationAdvanced(4,"Tangshan"),         new StationAdvanced(5,"Beidaihe"),         new StationAdvanced(6,"Qinhuangdao"),         new StationAdvanced(7,"Langfang"),         new StationAdvanced(8,"Tianjin"), };








/// <summary>
/// "Xiaobai Learning Program" Lesson 10: Two-way List (Linked-List) - Algorithm for Jumping the Line
/// </summary>
/// <param name="sender">< /param>
/// <param name="e"></param>
private void button10_Click(object sender, EventArgs e)
{     // #1 Create train information flexibly; choose three stations (Beijing, Xianghe, Tangshan) to participate calculate;

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

    StringBuilder sb = new StringBuilder();

    // #2 Construct train number information (direct)
    // Set the next stop in Beijing to Tangshan
    bj.Next = ts;
    // Set the last stop in Tangshan to Beijing 
    ts.Last = bj;

    // Output train number information (forward direction)
    // Depart from Beijing
    StationAdvanced start = bj;
    while (start != null)
    {         sb.AppendLine(start.Id + " " + start.Name + "<br>");         start = start. Next;     }


    // #3 Construct more train number information at stations
    // It is equivalent to inserting Xianghe (jumping in line)
    bj.Next = xh;
    ts.Last = xh;
    xh.Next = ts;
    xh.Last = bj;

    // #4 Output train number information (forward direction)
    // Depart from Beijing
    start = bj;
    sb.AppendLine("Train number information (forward direction)<br>");
    while (start != null)
    {         sb.AppendLine(start. Id + " " + start.Name + "<br>");         start = start.Next;     }     sb.AppendLine("<br>");



    // #5 Output train information (reverse)
    // Depart from Tangshan
    start = ts;
    sb.AppendLine("Train information (reverse)<br>");
    while (start != null)
    {         sb.AppendLine(start. Id + " " + start.Name + "<br>");         start = start.Last;     }     webBrowser1.DocumentText = sb.ToString();




}
 

2 code format


/// <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();

}

Guess you like

Origin blog.csdn.net/beijinghorn/article/details/132523245