C#、「Xiaobi 学習プログラム」レッスン 10: 双方向リスト (リンク リスト) 1、リンク リストを使用して高速鉄道の列車情報を作成する (ネイティブ アルゴリズム)

1 テキスト形式


/// <summary>
/// ステーション情報クラスの改善
/// リンクされたリストに必要な 2 つの属性を追加しました Last Next
/// </summary>
public class StationAdvanced
{     /// <summary>     /// number     / // </summary>     public int Id { get; set; } = 0;     /// <summary>     /// ステーション名     /// </summary>     public string Name { get; set; } = string.Empty;     public StationAdvanced最後 { 取得; 設定; } = null;     public StationAdvanced 次の { 取得; 設定; } = null;









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



// リストの初期値 List
<StationAdvanced> station_advanced = new List<StationAdvanced>() {         new StationAdvanced(1,"北京"),         new StationAdvanced(2,"石家荘"),         new StationAdvanced(3,"翔河" )、         新しい StationAdvanced(4,"唐山")、         新しい StationAdvanced(5,"北戴河")、         新しい StationAdvanced(6,"秦皇島")、         新しい StationAdvanced(7,"廊坊")、         新しい StationAdvanced(8,"天津") )、};








/// <summary>
/// 「Xiaobi 学習プログラム」 レッスン 10: 双方向リスト (リンクリスト) - 優先順位を決めるアルゴリズム
/// </summary>
/// <param name="sender"> </ param>
/// <param name="e"></param>
private void button10_Click(object sender, EventArgs e)
{     // #1 列車情報を柔軟に作成し、3 つの駅 (北京、香河、唐山) を選択して、参加して計算します。

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

    StringBuilder sb = 新しい StringBuilder();

    // #2 列車番号情報の構築 (直接)
    // 北京の次の停留所を唐山に設定
    bj.Next = ts;
    // 唐山の終点を北京に設定 
    ts.Last = bj;

    // 列車番号情報を出力(順方向)
    // 北京駅発
    Advanced start = bj;
    while (start != null)
    {         sb.AppendLine(start.Id + " " + start.Name + "<br>");         start = 開始。次へ;     }


    // #3 駅でさらに多くの列車番号情報を構築する
    // Xianghe (一直線に飛び込む) を挿入するのと同じ
    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