将项目添加和排序到单个链接列表中

时间:2012-12-06 16:27:22

标签: c# class

我正在学习如何使用课程。我做了两个班,一个是汽车列表。但是,我需要修改添加功能,以便添加按价格排序的汽车。我遇到的问题是,它会将最便宜的汽车发送到开头但杀掉列表的其余部分。这是我添加的代码......

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

3 个答案:

答案 0 :(得分:3)

要将项目插入单个链接列表,您需要:

  1. 将以前的节点next(或first(如果它是第一项)更改为指向新节点。
  2. 将新项目的next更改为新项目之前项目的next。 (你的代码中没有这样做。)
  3. 如果你有一个双向链表(你似乎没有),你还需要:

    1. 将当前节点previous更改为指向您之前的节点。
    2. 将下一个节点的previous更改为指向您。
    3. 请注意,这些操作可能需要按照我在此处指定的顺序完成。

      由于您还有一个last指针,您需要检查是否需要更新并更新它。

      您遇到的另一个问题是您使用last在第一项之后添加任何内容。你......不想这样做。您需要遍历列表,这意味着创建一个新的局部变量来跟踪当前位置。实际上,您的代码基本上消除了last中的任何内容。

答案 1 :(得分:0)

您已正确识别案例:

  1. 列表是否仍为空?
  2. 是否应该在开头添加项目?
  3. 如果不是:我们需要在哪个项目之后插入?
  4. 你实施了第一个案例。

    第二种情况是错误的:在开头插入意味着将第一个元素更改为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++;
        }
    }
    

答案 2 :(得分:0)

如果您想使用LinkedList Class,以下是基于您的方案的示例实现:

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);
    }
}