0

私はクラスで働く方法を学んでいます。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++;
4

3 に答える 3

3

単一リンクリストにアイテムを挿入するには、次のことを行う必要があります。

  1. 以前のノードnext(またはfirstそれが最初のアイテムの場合)を変更して、新しいノードを指すようにします。
  2. next新しいアイテムのを、新しいアイテムnextの直前のアイテムに使用されていたものに変更します。(コードではこれを行っていません。)

二重にリンクされたリストがある場合(表示されない場合)、次のことも行う必要があります。

  1. 現在のノードを変更してprevious、前のノードを指すようにします。
  2. 次のノードpreviousを自分を指すように変更します。

これらの操作は、ここで指定した順序以外の順序で実行する必要がある場合があることに注意してください。

ポインタもあるので、lastそれを更新する必要があるかどうかを確認して更新する必要があります。

あなたが持っているもう一つの問題は、あなたがlast最初のアイテムの後に何かを追加するために使用しているということです。あなたは...そんなことをしたくありません。リストをトラバースする必要があります。これは、現在の位置を追跡するために新しいローカル変数を作成することを意味します。現状では、コードは基本的にに含まれていたものをすべて消去していlastます。

于 2012-12-06T16:36:44.570 に答える
0

LinkedListクラスを使用する場合は、シナリオに基づいた実装例を次に示します。

class CarList : LinkedList<Car>
{
    public void AddCar(Car newCar)
    {
        if (this.Count == 0)
        {
            AddFirst(newCar);
        }
        else
        {
            var referenceCar = Find(this.OrderByDescending(i => i.Price).Where(i => newCar.Price > i.Price).FirstOrDefault());
            if (referenceCar == null)
            {
                AddBefore(First, newCar);

            }
            else
            {
                this.AddAfter(referenceCar, newCar);
            }
        }
    }
}

class Car
{
    public int Price { get; set; }
    public Car(int price)
    {
        Price = price;
    }
}

static void Main(string[] args)
{
    var list = new CarList();
    list.AddCar(new Car(20000));
    list.AddCar(new Car(10000));
    list.AddCar(new Car(15000));

    foreach (var item in list)
    {
        Console.WriteLine("Price {0}", item.Price);
    }
}
于 2012-12-06T16:52:34.450 に答える
0

ケースを正しく識別しました:

  1. リストはまだ空ですか?
  2. アイテムは最初に追加する必要がありますか?
  3. そうでない場合: どの項目の後に挿入する必要がありますか?

最初のケースを問題なく実装しました。

2 番目のケースは間違っています。先頭に挿入すると、最初の要素が に変更されますが、前の最初の要素を指す必要があります。そうしないと、リンクが失われますnew_carnew_car.Next

3 番目のケースも間違っています。最後の要素 (後で挿入する必要がある要素) に到達するか、後続要素がより大きな値を持つ要素が見つかるまで、リストの最後に移動する必要があります。価格とその後の挿入。

whileそのような条件を付けることができる理由は、current != lastがあると確信できる場合current.Next、そうでない場合はlast定義上そうなるからです。一時的な反復要素が必要な理由は、変更するfirstとリストへのエントリ ポイントが失われるためです。

次のコードをテストしていない場合でも、手がかりが得られるはずです。それが機能しない場合は、シングルステップ デバッグが役に立ちます。

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
            new_car.Next = first; // Insert the first car as the first element
            first = new_car;
        }
        else
        {
            // Create temporary iteration element
            the_cars current = first;

            // Find the car
            while (current != last && new_car.getPrice() >= current.Next.getPrice())
                current = current.Next;

            // Insert after the given element
            new_car.Next = current.Next;
            current.Next = new_car;

            // Also you may need to update last to match the new end
            if (current == last)
                last = new_car;
        }

        count++;
    }
}
于 2012-12-06T16:50:03.847 に答える