1

This is my code can someone convert it from while to do while in c thank you so much!!

for(int i=1;i<=31;++i){
            if(hashTables[i]!=NULL){
                printf ("Index %d: %s",i,hashTables[i]->value);
                data *current=hashTables[i];

            while(current->next!=NULL){
                printf (" -> %s",current->next->value);
                current=current->next;
            }
                printf ("\n");
            }   
        }
Matthew
  • 11
  • 3
  • 2
    In your *specific* case, it's trivial: replace the `do` with your entire `while` line (except for the semicolon), then delete that `while` line. (This won't always work in other cases, though.) – Adrian Mole Apr 14 '20 at 17:51
  • 1
    It works *primarily* because you set the test condition, `empty==false` to be correct for the loop to run *immediately* before the first loop, in the `empty=false`;` statement. See here: ['do...while' vs. 'while'](https://stackoverflow.com/questions/3347001/do-while-vs-while). – Adrian Mole Apr 14 '20 at 17:56
  • This code is going to run off the end of the array. The loop itself needs to stop when `key+i` reaches `m`. You would get much better answers if you posted a [mcve]. – user3386109 Apr 14 '20 at 18:21

2 Answers2

1
int i=0;
        bool empty;
        empty=false;

            if(ND[key+i]==NULL){
                empty=true;
            }
            else{
                ++i;
            }

        while(empty==false)
            {
                if(ND[key+i]==NULL){
                empty=true;
            }
            else{
                ++i;
            }
            }
        if(key+i<m){
            ND[key+i]=N_Data;
            ND[key+i]->step=i;
        }
    }
Muzaffer
  • 5
  • 4
0

Just replace the do line with the while line of your source code:

while (empty == false) 
{     
   if (ND[key+i] == NULL)
   {
      empty = true;
   }
   else
   {
      ++i;
   }
}