В настоящее время я изучаю, как создать собственный связанный список на C#. Я создал функцию под названием AddTrees
, которая добавляет дерево в конец списка в соответствии с введенными текстовыми полями: tree_name
, tree_height
, tree_price
и tree_instock
. Я прошу помощи в том, как я могу модифицировать мою функцию для реализации способа вставки дерева между текущим и current.next_tree
?Вставить элементы в объединенный список
Пример
public class TheTrees
{
private string tree_type = " ";
private int tree_height = 0;
public double tree_price = 0;
private int tree_instock = 0;
public TheTrees next_tree;
public TheTrees(string newtree, int newheight, int newinstock, double newprice)
{
tree_type = newtree;
tree_height = newheight;
tree_price = newprice;
tree_instock = newinstock;
next_tree = null;
}
public override string ToString()
{
return tree_type + " " + tree_height + " " +
tree_price + " " + tree_instock;
}
}
public class ListForTrees
{
public TheTrees first_tree;
public TheTrees last_tree;
public int count = 0;
public ListForTrees(TheTrees new_tree)
{
first_tree = new_tree;
last_tree = new_tree;
count = 1;
}
public ListForTrees()
{
}
public void AddTree(TheTrees new_tree)
{
TheTrees current = first_tree;
if (count == 0)
{
first_tree = new_tree;
last_tree = new_tree;
count = 1;
}
else if (count != 0)
{
if (new_tree.tree_price <= first_tree.tree_price)
{
new_tree.next_tree = first_tree;
first_tree = new_tree;
}
else if (new_tree.tree_price >= last_tree.tree_price)
{
last_tree.next_tree = new_tree;
last_tree = new_tree;
}
else
{
while (new_tree.tree_price > current.next_tree.tree_price)
{
current = current.next_tree;
}
new_tree.next_tree = current.next_tree;
current.next_tree = new_tree;
}
count++;
}
}
public void ClearTrees()
{
first_tree = null;
count = 0;
}
}
Этот вид помогает, но не очень. деревья создаются из пользовательских входов textBoxes – CodingWonders90
Возможно, я не понимаю проблему. Я, конечно, не понимаю, что это касается текстовых полей, которые делают это решение неприменимым. С другой стороны, я не понимаю, почему вы все равно хотите связанный список, а не обычный «Список». –
tallseth
Я рад добавить несколько полезных деталей, если я могу понять, что еще вы ищете. – tallseth