给定一个创建的带有货物的链表,我需要使用函数检查实际序列或"train"是否有效

Given a created linked list with cargos, I need to check if the actual sequence or "train" is valid using a function

本文关键字:检查 函数 是否 有效 train 一个 创建 链表      更新时间:2023-10-16

您好,我正在做一个项目,我需要比较并检查我制作的火车是否有效,条件如下:

-如果满足以下任何条件,则列车被视为无效:

-氧化剂货物不能与可燃货物相邻

-3 可燃车不能排成一排

-5辆生物车不能排成一排

-生物汽车只能在一侧具有放射性(不能具有放射性->生物->放射性(

我的问题是,我应该如何

实现它以正确匹配条件?,我应该如何修复它?。 这段代码没有错误,只是它与我想要它做的事情不匹配,我得到了我需要做的事情,尽管无法正确实现它。谢谢。

这是我到目前为止所拥有的:

enum CARGO_TYPE { //All of the possible cargo types (or car types)
  BIOLOGICAL,
  POISONOUS,
  COMBUSTIBLE,
  OXIDIZER,
  RADIOACTIVE,
  LOCOMOTIVE,
  CABOOSE
};
// Name: isValid
// PreCondition:  none
// PostCondition: returns a boolean indicating whether the current
//                arrangement of the train is valid
bool Train :: isValid() const{
Car *current = m_head;
Car *traverse = current->getnext();
  //iterates over the linked list, here's my logic
  while(current->getNext() != NULL){
    Car *temp = new Car(cargo)
    //checks the cargo sequence for OXIDIZER adjacent to COMBUSTIBLE
    if(current->getNext()->getType() == OXIDIZER && traverse->getNext->getType() == COMBUSTIBLE){
      return false;
    }
    //checks the 3 COMBUSTIBLE in a row, just sub in COMBUSTIBLE inside
    if(traverse == traverse->getNext()== traverse->getNext()->getNext()){
      return false;
    }
    //checsk the 5 BIOLOGICAL cars in a row
    if((traverse == traverse->getNext())== traverse->getNext()->getNext()){
      return false;
    }
    // checks if BIOLOGICAL only have one RADIOACTIVE on one side.
    if(traverse == traverse->getNext()== traverse->getNext()->getNext()){
    }
  return true;
  }
}
按照

你之前的做法,你可以试试这个......虽然最初没有编译以检查它。

bool Train :: isValid() const
{
  Car *current = m_head;
  Car *traverse;
  //iterates over the linked list
  while(current->getNext() != NULL)
  {
    traverse = current->getNext();
    //checks the cargo sequence for OXIDIZER adjacent to COMBUSTIBLE
    // Need to check both ways, -O-C- and -C-O-
    if((current->getType() == OXIDIZER && traverse->getType() == COMBUSTIBLE) || 
       (current->getType() == COMBUSTIBLE && traverse->getType() == OXIDIZER)){
      return false;
    }
    //checks the 3 COMBUSTIBLE in a row, just sub in COMBUSTIBLE inside
    if(current->getType() == COMBUSTIBLE && traverse->getType() == COMBUSTIBLE){
      //Need the inner conditional to make sure traverse isnt the 
      //end of the train
      if(traverse->getNext() != NULL && traverse->getNext()->getType() == COMBUSTIBLE){
         return false;
       }
    }
  //check the 5 BIOLOGICAL cars in a row
  if(current->getType() == BIOLOGICAL && traverse->getType() == BIOLOGICAL)
  {
     int count = 2; //already counted the first two in conditional above
     while(traverse->getNext() != NULL && count < 5)
     {
      traverse = traverse->getNext();
      if(traverse->getType() == BIOLOGICAL)
        count++; 
      else
        break;
     }
   if(count == 5)//if 5 biologicals were counted in a row, return false
     return false;
  }
  // checks if BIOLOGICAL only have one RADIOACTIVE on one side.
  if(current->getType() == RADIOACTIVE && traverse->getType() == BIOLOGICAL) 
    if(traverse->getNext() != NULL && traverse->getNext()->getType() == RADIOACTIVE)
      return false
    current = traverse; //continue iterating through train
  }
  return true;//passes everything, train should be good
}

使用风险自负,有更好的方法来做你想做的事......只是给出了与您的想法相似的东西。