我正在学习如何使用课程。我做了两节课,一节课是汽车清单。但是,我需要修改 add 函数,以便添加按价格排序的汽车。我遇到的问题是它会将最便宜的汽车送到开头,但会杀死列表的其余部分。这是我的添加代码...
public void add_car(the_cars new_car)
{// Method to add cars to list
if (count == 0)
{// If this is the first car
first = new_car;
last = new_car;
count = 1;
}
else
{// If it is not the first car
if (new_car.getPrice() < first.getPrice())
{// If price of new car is lower than first car
last = first;
first = new_car; // new car becomes first car
}
else
{
while (new_car.getPrice() > last.getPrice() || last.next != null)
{
last.next = new_car; // Null value now equal to car
last = new_car;
}
}
count++;