我正在使用链接列表。BtnAddTree
我的构造函数采用其中包含项目的数组或通过单击将一个项目(一次一个)附加到列表中。出于实验原因,我试图找出一种从列表中检索项目位置的方法。我已经设置了三个希望显示的按钮:first
, next
, last
item 和 display through a label
。我创建了两个函数来满足我的需要Retrieve
,Current_Tree()
但在这里我走到了死胡同。我知道要显示next
我可能需要使用循环但不确定如何进行的项目。如何显示在列表中找到的项目的位置?
public class ListForTrees
{
//Retrieve Position of item
public void Retrieve(int Position)
{
int new_position = Position;
fruit_trees current = first_tree;
for (int i = 0; i < Position && current != null; i++)
{
current = current.next_tree;
}
return current;
}
//Show Current Tree
public void Current_Tree()
{
try
{
current = fruit_trees.first_tree;
labelSpecificTree.Text = current.Type.ToString();
}
catch { };
}
}
ListForTrees mainlist = new ListForTrees();
private void BtnGo_Click(object sender, EventArgs e)
{
fruit_trees[] ax = { new fruit_trees("cherry", 48, 12.95, 3),
new fruit_trees("pine", 36, 9.95, 8),
new fruit_trees("oak", 60, 14.95, 2),
new fruit_trees("peach", 54, 19.95, 3),
new fruit_trees("pear", 36, 11.85, 2),
new fruit_trees("apple", 62, 13.45, 5)
};
mainlist = new ListForTrees(ax);
fruit_trees current = mainlist.first_tree;
while (current != null)
{
TxtOutput.AppendText(current.ToString() + Environment.NewLine);
current = current.next_tree;
}
}
private void ShowNextItem_Click(object sender, EventArgs e)
{
//Show Next Item
}
private void ShowLastItem_Click(object sender, EventArgs e)
{
//Show Last Item
}
private void ShowFirstItem_Click_1(object sender, EventArgs e)
{
// Show First Item
}
}
}