LinkedList RemovePosition删除错误的元素

时间:2019-04-16 08:26:40

标签: c++ linked-list

我正在创建一个模板化LinkedList,并使用它来模拟火车经过站点。 ResidentType.php public function buildForm(FormBuilderInterface $builder, array $options) { $sexe = array( 'Homme' => 'Homme', 'Femme' => 'Femme' ); $alzheimer = array( 'Oui' => 'Oui', 'Non' => 'Non' ); $builder->add('nomResident',TextType::class,array('label'=>'Nom:')) ->add('prenomResident',TextType::class,array('label'=>'Prénom:')) ->add('ageResident') -> add('sexeResident',ChoiceType::class,array( 'choices'=>$sexe, 'expanded'=>true, 'label'=>'Sexe:' )) -> add('alzheimerResident',ChoiceType::class,array( 'choices'=> $alzheimer, 'expanded'=>true, 'label'=>'Alzheimer' )) ->add('maladieResident',TextareaType::class,array('label'=>'Maladie:')) ->add('responsable',TextType::class,array('label'=>'Responsable:')) ->add('telephoneResponsable',TelType::class,array('label' => 'Numero Telephone : ')) ->add('maison', EntityType::class,array( 'class'=>'MaisonretraiteBundle:Maison', 'choice_label'=>'nom_maison', 'multiple'=>false, 'label' => 'Choisissez votre maison : ' ))->add('dateResident',DateType::class,array('disabled'=>'true')); } 的每个元素都是一个LinkedList变量的TrainCar类的对象。如果此值停止时等于1,则numberOfStops应该从列表中删除该元素。但是,最终会删除它之前的元素。

我尝试同时修改LinkedList函数和通过此函数传递的值,似乎没有什么可以解决问题

这是我的RemovePosition类中的RemovePosition()函数

LinkedList

这是我的template <class T> void LinkedList<T>::RemovePosition(int index) { if(index == 0) { RemoveFromFront(); } else if(index == (size - 1)) { RemoveFromEnd(); } else { node<T>* temp1 = head; for(int i = 0; i < index - 1; i++) { temp1 = temp1->next; } node<T>* temp2 = temp1->next; temp1->next = temp2->next; size--; } } 函数代码,发生了错误

main()

这是我当前的输出

for(j = 1; j <= stops; j++) {
    cout << "Stop #" << j << ":" << endl;
    cout << "Train Arriving: ";

    for(i = 0; i < train.size; i++) {
      type = train.Retrieve(i).typeOfCar;
      id = train.Retrieve(i).finalID;
      numberOfStops = train.Retrieve(i).numberOfStops;
      cout << "[" << id << ":" << type << ":" << numberOfStops << "] ";
    }

    cout << endl << "Removing cars:" << endl;

    for(i = 0; i < train.size; i++) {
      numberOfStops = train.Retrieve(i).numberOfStops;
      if(numberOfStops == 1) {
        id = train.Retrieve(i).finalID;
        type = train.Retrieve(i).typeOfCar;
        cout << "[" << id << ":" << type << "] removed" << endl;
        if(train.Retrieve(i).typeOfCar == 'P') {
          addCargoCar--;
        }
        train.RemovePosition(i);
      }
    }
}

如您所见,错误的Stop #1: Train Arriving: [9:P:4] [5:P:2] [3:P:2] [10:C:2] [8:C:1] [1:C:2] [2:M:1] [4:M:5] [6:M:4] [7:M:1] Removing cars: [8:C] removed [2:M] removed [7:M] removed Adding cars: [11:P:2] added [12:C:4] added [13:P:4] added [14:M:2] added [15:M:2] added Stop #2: Train Arriving: [13:P:4] [11:P:2] [9:P:4] [5:P:2] [3:P:2] [12:C:4] [8:C:1] [2:M:1] [4:M:5] [7:M:1] [14:M:2] [15:M:2] 被取出。使用这些相同的输入,列车到达2号车站的正确结果将是:

TrainCar

编辑1:实现@leJohn提供的解决方案,现在我收到一个分段错误。我认为这必须与我对Stop #1: Train Arriving: [9:P:4] [5:P:2] [3:P:2] [10:C:2] [8:C:1] [1:C:2] [2:M:1] [4:M:5] [6:M:4] [7:M:1] Removing cars: [8:C] removed [2:M] removed [7:M] removed Adding cars: [11:P:2] added [12:C:4] added [13:P:4] added [14:M:2] added [15:M:2] added Stop #2: Train Arriving: [13:P:4] [11:P:2] [9:P:4] [5:P:2] [3:P:2] [12:C:4] [10:C:2] [1:C:2] [4:M:5] [6:M:4] [14:M:2] [15:M:2] 函数的调用有关,因为当我在RemoveFromEnd()函数中将if条件设置为if(numberOfStops == 2)时,此解决方案就会起作用。这是我的main()函数

RemoveFromEnd()

我不明白为什么这会导致错误...

1 个答案:

答案 0 :(得分:0)

替换循环:

for(int i = 0; i < index - 2; i++) {
      temp1 = temp1->next;
    }

作者

for(int i = 0; i < index - 1; i++) {
      temp1 = temp1->next;
    }

应该可以解决问题