///
/// 改进的车站信息类 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 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("插入新车站:
");
// 北京 出发
StationAdvanced start = bj;
while (start != null)
{
sb.AppendLine(start.Id + " " + start.Name + "
");
start = start.Next;
}
sb.AppendLine("
");
// 删除 香河
StationRemove(xh);
// #4 输出车次信息(正向)
// 北京 出发
sb.AppendLine("删除指定车站:
");
start = bj;
while (start != null)
{
sb.AppendLine(start.Id + " " + start.Name + "
");
start = start.Next;
}
webBrowser1.DocumentText = sb.ToString();
}
///
/// 双向链表的插入算法
/// 将 c 节点插入 a (之后) b 之间
///
///
///
private void StationInsert(StationAdvanced a, StationAdvanced c)
{
StationAdvanced b = a.Next;
a.Next = c;
c.Last = a;
c.Next = b;
b.Last = c;
}
///
/// 双向链表的删除算法
/// 删除 a (之后) 的 b 节点
///
///
private void StationRemove(StationAdvanced b)
{
StationAdvanced a = b.Last;
StationAdvanced c = b.Next;
a.Next = c;
c.Last = a;
}
///
/// 改进的车站信息类 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 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("插入新车站:
");
// 北京 出发
StationAdvanced start = bj;
while (start != null)
{
sb.AppendLine(start.Id + " " + start.Name + "
");
start = start.Next;
}
sb.AppendLine("
");
// 删除 香河
StationRemove(xh);
// #4 输出车次信息(正向)
// 北京 出发
sb.AppendLine("删除指定车站:
");
start = bj;
while (start != null)
{
sb.AppendLine(start.Id + " " + start.Name + "
");
start = start.Next;
}
webBrowser1.DocumentText = sb.ToString();
}
///
/// 双向链表的插入算法
/// 将 c 节点插入 a (之后) b 之间
///
///
///
private void StationInsert(StationAdvanced a, StationAdvanced c)
{
StationAdvanced b = a.Next;
a.Next = c;
c.Last = a;
c.Next = b;
b.Last = c;
}
///
/// 双向链表的删除算法
/// 删除 a (之后) 的 b 节点
///
///
private void StationRemove(StationAdvanced b)
{
StationAdvanced a = b.Last;
StationAdvanced c = b.Next;
a.Next = c;
c.Last = a;
}