• 全部
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm&WPF
  • C#开发新技术
  • 问答

求一段代码,简单链表结构

hejiwang 2006-06-02 10:17:57
假如有如下数据:
id curr next
1 A B
1 B C
1 C D
1 D E
1 E F
假如知道ID=1和curr=B,求最后一个节点E

请用c#写,谢谢大家!
...全文
206 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
chenyuming2004 2006-06-02
DataTable dt = new DataTable();

dt.Columns.Add("ID");
dt.Columns.Add("CURR");
dt.Columns.Add("NEXT");

DataRow dr1 = dt.NewRow();
dr1["ID"] = "1";
dr1["CURR"] = "A";
dr1["NEXT"] = "B";
dt.Rows.Add(dr1);

dr1 = dt.NewRow();
dr1["ID"] = "1";
dr1["CURR"] = "B";
dr1["NEXT"] = "C";
dt.Rows.Add(dr1);

dr1 = dt.NewRow();
dr1["ID"] = "1";
dr1["CURR"] = "C";
dr1["NEXT"] = "D";
dt.Rows.Add(dr1);

dr1 = dt.NewRow();
dr1["ID"] = "1";
dr1["CURR"] = "D";
dr1["NEXT"] = "E";
dt.Rows.Add(dr1);

dr1 = dt.NewRow();
dr1["ID"] = "1";
dr1["CURR"] = "E";
dr1["NEXT"] = "F";
dt.Rows.Add(dr1);

string strCurr = "B";
DataRow[] drs = dt.Select("CURR='" + strCurr + "'");

for (;drs.Length != 0;)
{
if (drs[0]["curr"].ToString() == "E")
{
Response.Write(drs[0]["curr"].ToString());
break;
}
strCurr = drs[0]["next"].ToString();
drs = dt.Select("CURR='" + strCurr + "'");
}
回复
lookatliu 2006-06-02
DataTable dt = new DataTable();

dt.Columns.Add("ID");
dt.Columns.Add("CURR");
dt.Columns.Add("NEXT");

DataRow dr1 = dt.NewRow();
dr1["ID"] = "1";
dr1["CURR"] = "A";
dr1["NEXT"] = "B";
dt.Rows.Add(dr1);

dr1 = dt.NewRow();
dr1["ID"] = "1";
dr1["CURR"] = "B";
dr1["NEXT"] = "C";
dt.Rows.Add(dr1);

dr1 = dt.NewRow();
dr1["ID"] = "1";
dr1["CURR"] = "C";
dr1["NEXT"] = "D";
dt.Rows.Add(dr1);

dr1 = dt.NewRow();
dr1["ID"] = "1";
dr1["CURR"] = "D";
dr1["NEXT"] = "E";
dt.Rows.Add(dr1);

dr1 = dt.NewRow();
dr1["ID"] = "1";
dr1["CURR"] = "E";
dr1["NEXT"] = "F";
dt.Rows.Add(dr1);

string strCurr = "B";

DataTable dtTemp = dt.Copy();

while(dtTemp.Rows.Count > 0)
{
DataRow[] drTemp = dtTemp.Select("CURR = '" + strCurr + "'");

if (drTemp.Length == 0)
{
break;
}
else
{
strCurr = drTemp[0]["NEXT"].ToString();

foreach(DataRow dr in drTemp)
{
dtTemp.Rows.Remove(dr);
}
}
}

DataRow[] drSelect = dt.Select("NEXT = '" + strCurr + "'");
回复
hejiwang 2006-06-02
为什么没有人理我啊!
回复
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2006-06-02 10:17
社区公告

让您成为最强悍的C#开发者