///
/// 改进的车站信息类 class
/// 增加了 链表 需要的两个属性 Last Next
///
public class StationAdvanced
{
///
/// 编号
///
public int Id { get; set; } = 0;
///
/// 车站名
///
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
new StationAdvanced(1,"北京"),
new StationAdvanced(2,"石家庄"),
new StationAdvanced(3,"香河"),
new StationAdvanced(4,"唐山"),
new StationAdvanced(5,"北戴河"),
new StationAdvanced(6,"秦皇岛"),
new StationAdvanced(7,"廊坊"),
new StationAdvanced(8,"天津"),
};
///
/// 《小白学程序》第十课:双向列表(Linked-List)——插队的算法
///
///
///
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 + "
");
start = start.Next;
}
// #3 构造更多车站的车次信息
// 等于将 香河 插入(插队)
bj.Next = xh;
ts.Last = xh;
xh.Next = ts;
xh.Last = bj;
// #4 输出车次信息(正向)
// 北京 出发
start = bj;
sb.AppendLine("车次信息(正向)
");
while (start != null)
{
sb.AppendLine(start.Id + " " + start.Name + "
");
start = start.Next;
}
sb.AppendLine("
");
// #5 输出车次信息(反向)
// 唐山 出发
start = ts;
sb.AppendLine("车次信息(反向)
");
while (start != null)
{
sb.AppendLine(start.Id + " " + start.Name + "
");
start = start.Last;
}
webBrowser1.DocumentText = sb.ToString();
}
2 代码格式
///
/// 改进的车站信息类 class
/// 增加了 链表 需要的两个属性 Last Next
///
public class StationAdvanced
{
///
/// 编号
///
public int Id { get; set; } = 0;
///
/// 车站名
///
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 stations_advanced = new List() {
new StationAdvanced(1,"北京"),
new StationAdvanced(2,"石家庄"),
new StationAdvanced(3,"香河"),
new StationAdvanced(4,"唐山"),
new StationAdvanced(5,"北戴河"),
new StationAdvanced(6,"秦皇岛"),
new StationAdvanced(7,"廊坊"),
new StationAdvanced(8,"天津"),
};
///
/// 《小白学程序》第十课:双向列表(Linked-List)——插队的算法
///
///
///
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 + "
");
start = start.Next;
}
// #3 构造更多车站的车次信息
// 等于将 香河 插入(插队)
bj.Next = xh;
ts.Last = xh;
xh.Next = ts;
xh.Last = bj;
// #4 输出车次信息(正向)
// 北京 出发
start = bj;
sb.AppendLine("车次信息(正向)
");
while (start != null)
{
sb.AppendLine(start.Id + " " + start.Name + "
");
start = start.Next;
}
sb.AppendLine("
");
// #5 输出车次信息(反向)
// 唐山 出发
start = ts;
sb.AppendLine("车次信息(反向)
");
while (start != null)
{
sb.AppendLine(start.Id + " " + start.Name + "
");
start = start.Last;
}
webBrowser1.DocumentText = sb.ToString();
}