簡體   English   中英

將項目添加並排序到單鏈列表中

[英]adding and sorting items into a singly linked list

我正在學習如何上課。 我上了兩堂課,一堂是汽車清單。 但是,我需要修改添加功能,以便它將添加按價格排序的汽車。 我遇到的問題是,它將把最便宜的汽車送到起點,但殺死其余的汽車。 這是我添加的代碼...

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++;

要將項目插入單鏈列表,您需要:

  1. 改變以前的節點next (或first ,如果它的第一項),以指向新的節點。
  2. 更改next新項目將成為怎樣的next使用的只是你的新項目之前的項目。 (您沒有在代碼中這樣做。)

如果您有一個雙向鏈接列表(您似乎沒有,則沒有),您還需要:

  1. 更改previous的當前節點以指向您之前的節點。
  2. 更改下一個節點的previous以指向您。

請注意,這些操作可能需要按照我在此處指定的順序以外的順序進行。

由於您還有last指針,因此需要檢查是否需要更新並更新它。

您遇到的另一個問題是您正在使用last在第一項之后添加任何內容。 你...不想那樣做。 您需要遍歷列表,這意味着創建一個新的局部變量來跟蹤當前位置。 實際上,您的代碼基本上清除了last

您已正確識別出案例:

  1. 列表是否仍然為空?
  2. 是否應在開始時添加項目?
  3. 如果不是:我們需要在哪個項目之后插入?

您實現了第一種情況。

第二種情況是錯誤的:在開頭插入意味着將第一個元素更改為new_car ,但是new_car.Next需要指向上一個第一個元素,否則將丟失鏈接。

第三種情況也是錯誤的:您需要轉到列表的末尾,直到到達最后一個元素(然后是您需要在其后插入的元素),或者直到找到其后繼元素更大的元素為止。價格,然后插入。

之所以可以設置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++;
    }
}

如果您想使用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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM