尽管有停止条件,仍然执行无限递归调用,因为参数不会前进

时间:2015-04-12 22:48:59

标签: c++ recursion structure

我有一个递归函数,我需要滚动一组结构,这些结构是连续的子列表形式。

节点结构

struct Intake{ // this is a sublist
   int id;
   string month;
   int intakeEnergy;
   struct Intake * next;
   struct electricalDevices * firstED;
   struct intakeEnergySector * linkIES;
   Intake(int i, string m, int iE){
       id = i;
       month = m;
       intakeEnergy = iE;
       next = NULL;
       firstED = NULL;
   }
};

struct Home{   // this is a sublist
   int code;
   string address;
   struct Home * next;
   struct Intake * firstI;
   struct homePersons * linkHP;
   Home(int c, string ad){
       code = c;
       address = ad;
       next = NULL;
       firstI = NULL;
   }
};

致电&递归函数

int recursiveTraversal(int idLocality, struct Home *homeToRoam, struct Intake *intakeToRoam, int idElectricalDevice){
    if (homeToRoam == NULL)
        return 0;
    else{
        intakeToRoam = homeToRoam -> firstI;

        if (intakeToRoam == NULL)
            return 0 + (recursiveTraversal(idLocality, homeToRoam -> next, intakeToRoam, idElectricalDevice));
        else{
            struct electricalDevices * temp = searchED(idLocality, homeToRoam -> code, intakeToRoam -> id, idElectricalDevice);

            if (temp == NULL){
                return 0 + (recursiveTraversal(idLocality, homeToRoam, intakeToRoam -> next, idElectricalDevice));
            }else{

                return (temp -> intakeEnergyPerHour) + (recursiveTraversal(idLocality, homeToRoam, intakeToRoam -> next, idElectricalDevice));
            }
        }
    }
}

void monthlyConsumptionOfADeviceInALocality(int idLocality, int idElectricalDevice){
    struct Locality * localityX = searchL(idLocality);
    int monthlyConsumption;

    if (localityX == NULL){
        cout<<"\nThe Locality doesn't exist...\n";
        return ;
    }else{
        monthlyConsumption = recursiveTraversal(idLocality , localityX -> firstH, NULL, idElectricalDevice);
        cout<<"Monthly Consumption: "<<monthlyConsumption;
    }
}

这是函数的代码,问题是例如,当我分配homeToRome节点时,我写了localityX - &gt; firstH进入参数,进入递归函数,感觉相同,分配每个子列表的第一个节点。

增加线

  

返回0 +(recursiveTraversal(idLocality,homeToRoam, intakeToRoam - &gt; next ,idElectricalDevice));

但是当我将intakeToRoam增加到递归调用中的下一个节点时,它设置相同。

即使我打印了值存储器,intakeToRoam也是与intakeToRoam相比的不同值 - &gt;下。

事先非常感谢,感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您有无限递归,因为您始终使用以下行重置intakeToRoam

intakeToRoam = homeToRoam->firstI;

如果删除此行,则现在遇到识别迭代intakeToRoam列表的开始和结束的问题。解决此问题的最简单方法是保持NULL intakeToRoam意味着开始迭代,并手动检查函数中列表的结尾,如:

int recursiveTraversal(int idLocality, struct Home *homeToRoam, struct Intake *intakeToRoam, int idElectricalDevice){
    if (homeToRoam == NULL)
        return 0;
    else {

        if (intakeToRoam == NULL)
            return 0 + (recursiveTraversal(idLocality, homeToRoam, homeToRoam->firstI, idElectricalDevice));
        else {
            struct electricalDevices * temp = searchED(idLocality, homeToRoam -> code, intakeToRoam -> id, idElectricalDevice);
            int consumption = 0;

            if (temp != NULL) consumption = temp -> intakeEnergyPerHour;

            if (intakeToRoam->next != NULL)
                return consumption + recursiveTraversal(idLocality, homeToRoam, intakeToRoam -> next, idElectricalDevice);
            else
                return consumption + recursiveTraversal(idLocality, homeToRoam->next, NULL, idElectricalDevice);
        }
    }
}