私はクラスで働く方法を学んでいます。2 つのクラスを作成し、1 つは車のリストです。ただし、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++;