C# 将项目添加并排序到单链表中

C# 将项目添加并排序到单链表中,c#,class,C#,Class,我正在学习如何与班级合作。我上了两门课,一门是汽车清单。但是,我需要修改add函数,以便它可以添加按价格排序的汽车。我遇到的问题是,它会把最便宜的车送到最开始的地方,但会杀死名单上的其他车。这是我的代码添加 public void add_car(the_cars new_car) {// Method to add cars to list if (count == 0) {// If this is the first car

我正在学习如何与班级合作。我上了两门课,一门是汽车清单。但是,我需要修改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++;
public void添加车(新车)
{//将汽车添加到列表的方法
如果(计数=0)
{//如果这是第一辆车
第一辆=新车;
最后一辆=新车;
计数=1;
}
其他的
{//如果不是第一辆车
if(new_car.getPrice()last.getPrice()| | last.next!=null)
{
last.next=new_car;//空值现在等于car
最后一辆=新车;
}
}
计数++;

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

  • 将以前的节点
    next
    (或
    first
    如果是第一项)更改为指向新节点
  • 将新项目的
    next
    更改为新项目之前项目的
    next
    。(您的代码中没有这样做。)
  • 如果您有一个双链接列表(看起来您没有),您还需要:

  • 将当前节点
    previous
    更改为指向前面的节点
  • 更改下一个节点的
    上一个
    以指向您
  • 请注意,这些操作可能需要按照我在此处指定的顺序以外的顺序进行

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


    另一个问题是,您正在使用
    last
    在第一项之后添加任何内容。您……不想这样做。您需要遍历列表,这意味着创建一个新的局部变量来跟踪当前位置。事实上,您的代码基本上正在清除
    last
    中的内容> 要将项目插入单链表,您需要:

  • 将以前的节点
    next
    (或
    first
    如果是第一项)更改为指向新节点
  • 将新项目的
    next
    更改为新项目之前项目的
    next
    。(您的代码中没有这样做。)
  • 如果您有一个双链接列表(看起来您没有),您还需要:

  • 将当前节点
    previous
    更改为指向前面的节点
  • 更改下一个节点的
    上一个
    以指向您
  • 请注意,这些操作可能需要按照我在此处指定的顺序以外的顺序进行

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


    另一个问题是,您正在使用
    last
    在第一项之后添加任何内容。您……不想这样做。您需要遍历列表,这意味着创建一个新的局部变量来跟踪当前位置。事实上,您的代码基本上正在清除
    last
    中的内容> 您已正确识别了这些案例:

  • 名单还是空的吗
  • 是否应在开始时添加该项
  • 如果没有:我们需要在哪个项目之后插入
  • 你实施了第一个案例

    第二种情况是错误的:在开头插入意味着将第一个元素更改为
    new\u car
    ,但
    new\u car。下一个
    需要指向前一个元素,否则将丢失链接

    第三种情况也是错误的:您需要一直走到列表的末尾,直到您到达最后一个元素(即您需要在后面插入的元素),或者直到您找到一个后续元素,该元素的价格更高,然后再插入

    我之所以可以这样设置
    while
    条件,是因为如果
    current!=last
    我可以确定有一个
    current.Next
    ,否则根据定义它将是
    last
    。我需要一个临时迭代元素的原因是,如果我首先修改
    我将丢失列表的入口点

    如果您没有测试以下代码,但它应该给您一个线索,如果它不工作,单步调试将帮助您

    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++;
        }
    }
    
    public void添加车(新车)
    {//将汽车添加到列表的方法
    如果(计数=0)
    {//如果这是第一辆车
    第一辆=新车;
    最后一辆=新车;
    计数=1;
    }
    其他的
    {//如果不是第一辆车
    if(new_car.getPrice()=current.Next.getPrice())
    当前=当前。下一步;
    //在给定元素后插入
    new_car.Next=当前.Next;
    当前。下一个=新车;
    //此外,您可能需要更新last以匹配新的端点
    如果(当前==上次)
    最后一辆=新车;
    }
    计数++;
    }
    }
    
    您已正确识别案例:

  • 名单还是空的吗
  • 是否应在以下位置添加该项:
    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);
        }
    }