输入for循环后变量变空

时间:2015-09-30 13:55:05

标签: c++ list for-loop

        MA=ArrayToList(A);
        MB=ArrayToList(B);
        MC=ArrayToList(C);
        MD=ArrayToList(D);
        if(MB==NULL) cout<<"B is EMPTY"; // It's OK here
        for(;MA!=NULL;MA=MA->next){
            if(MB==NULL) cout<<"B is EMPTY"; // MB==NULL, same as MC
            bool f = true;
            for(;f && MB!=NULL;MB=MB->next)
                if(MA->elem==MB->elem) f = false;
            for(;f && MC!=NULL;MC=MC->next)
                if(MA->elem==MC->elem) f = false;
            if(f){
                mE=new mult;
                mE->elem=MA->elem;
                mE->next=ME;
                ME=mE;
            }
        }

问题是MB和MC在此字符串后立即变空

        for(;MA!=NULL;MA=MA->next){

有什么建议吗?

UPD: MB和MC在第一次迭代之前(在迭代体之前)变为空,而不是在它完成之后。 这就是MB的样子

typedef struct st
{
   char elem;
   struct st* next;
}mult;

这就是ArrayToList的作用:

mult* ArrayToList(char *Arr)
{
    mult *head=NULL, *phead;
    for (int i = 0; Arr[i]; i++)
    {
        phead = new mult;
        phead->elem = Arr[i];
        phead->next = head;
        head = phead;
    }
    return head;
}

我怎么看:

typedef struct st
{
   char elem;
   struct st* next;
}mult;

int main(){
//initialization of mult *MA, *MB, *MC, *MD;
//at this point all 4 variables are not empty and can be shown correctly
for(;MA!=NULL;MA=MA->next){
    //right after this point MB and MC somehow become empty, MA is still OK
    //some stuff
}
}

希望现在更清楚

0 个答案:

没有答案